STL passing vector fastly

G

Giulio

I'm doing a class who takes in constructor a vector<int> keeps and makes
some statistic on the vector whitout modifiing it.

I would like not to make a copy of the vector for the class. to let the full
process be faster a smaller in memory.

I tried to pass the vector by reference using * but that operator doesn't
exist.

my ask is: how the vector are passed to functions?

thanx alot

Giulio
 
G

Giulio

passing simply the vectors I need it makes me an error in the while
argument.
how to solve this?

thanx
Giulio
---------------------------------
std::vector<tipi::prezzo>::iterator pr = prezzo_.begin();
std::vector<tipi::eek:perazione>::iterator op = op_.begin();
std::vector<tipi::prezzo>::iterator finePr = prezzo_.end();
std::vector<tipi::eek:perazione>::iterator fineOp = op_.end();

while( op!=finePr && pr!=fineOp ) {

---------------------------------
Compiler: Default compiler
Building Makefile: "G:\bor\Makefile.win"
Executing make...
make.exe -f "G:\borsa\Makefile.win" all
g++.exe -D__DEBUG__ -c valuta.cpp -o
aluta.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C
:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" -g3

valuta.cpp: In member function `attendibilita valuta::getAttendibilita()':
valuta.cpp:33: no match for `__gnu_cxx::__normal_iterator<operazione*,
std::vector<operazione, std::allocator<operazione> > >& !=
__gnu_cxx::__normal_iterator<prezzo*, std::vector<prezzo,
std::allocator<prezzo> > >&' operator
C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(const
GUID&,
const GUID&)
valuta.cpp:33: no match for `__gnu_cxx::__normal_iterator<prezzo*,
std::vector<prezzo, std::allocator<prezzo> > >& !=
__gnu_cxx::__normal_iterator<operazione*, std::vector<operazione,
std::allocator<operazione> > >&' operator
C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(const
GUID&,
const GUID&)
valuta.cpp:45: warning: assignment to `int' from `attendibilita'
valuta.cpp:45: warning: argument to `int' from `attendibilita'

make.exe: *** [valuta.o] Error 1

Execution terminated
-----------------------------------
 
D

David White

Giulio said:
I'm doing a class who takes in constructor a vector<int> keeps and makes
some statistic on the vector whitout modifiing it.

I would like not to make a copy of the vector for the class. to let the full
process be faster a smaller in memory.

I tried to pass the vector by reference using * but that operator doesn't
exist.
my ask is: how the vector are passed to functions?

Example:
SomeClass::SomeClass(const vector<int> &v)
{
vector<int>::const_iterator iv = v.begin();
while(iv != v.end())
{
// do something
++iv;
}
}

David
 
D

David White

Giulio said:
passing simply the vectors I need it makes me an error in the while
argument.
how to solve this?

thanx
Giulio
---------------------------------
std::vector<tipi::prezzo>::iterator pr = prezzo_.begin();
std::vector<tipi::eek:perazione>::iterator op = op_.begin();
std::vector<tipi::prezzo>::iterator finePr = prezzo_.end();
std::vector<tipi::eek:perazione>::iterator fineOp = op_.end();

while( op!=finePr && pr!=fineOp ) {

---------------------------------
Compiler: Default compiler
Building Makefile: "G:\bor\Makefile.win"
Executing make...
make.exe -f "G:\borsa\Makefile.win" all
g++.exe -D__DEBUG__ -c valuta.cpp -o
luta.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C
:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" -g3

valuta.cpp: In member function `attendibilita valuta::getAttendibilita()':
valuta.cpp:33: no match for `__gnu_cxx::__normal_iterator<operazione*,
std::vector<operazione, std::allocator<operazione> > >& !=
__gnu_cxx::__normal_iterator<prezzo*, std::vector<prezzo,
std::allocator<prezzo> > >&' operator
C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(const
GUID&,
const GUID&)
valuta.cpp:33: no match for `__gnu_cxx::__normal_iterator<prezzo*,
std::vector<prezzo, std::allocator<prezzo> > >& !=
__gnu_cxx::__normal_iterator<operazione*, std::vector<operazione,
std::allocator<operazione> > >&' operator
C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(const
GUID&,
const GUID&)
valuta.cpp:45: warning: assignment to `int' from `attendibilita'
valuta.cpp:45: warning: argument to `int' from `attendibilita'

Looks like type mismatches, but it's hard to tell exactly where. Please post
the definitions of tipi::prezzo, tipi::eek:perazione, prezzo_ and op_ and it
might be easier to see what is wrong.

David
 
M

Mike Wahler

Giulio said:
I'm doing a class who takes in constructor a vector<int> keeps and makes
some statistic on the vector whitout modifiing it.

I would like not to make a copy of the vector for the class. to let the full
process be faster a smaller in memory.

I tried to pass the vector by reference using * but that operator doesn't
exist.

The * operator does indeed exist, but what you're after
is a reference:

#include <vector>

class X
{
public:
X(const std::vector<int>& arg) : v(arg) { }
private:
my ask is: how the vector are passed to functions?

You can pass them by value, by reference, or by pointer.

-Mike
 
M

MiniDisc_2k2

Giulio said:
I'm doing a class who takes in constructor a vector<int> keeps and makes
some statistic on the vector whitout modifiing it.

I would like not to make a copy of the vector for the class. to let the full
process be faster a smaller in memory.

I tried to pass the vector by reference using * but that operator doesn't
exist.

What you're saying is pass a reference to a pointer:

void f (std::vector<int>& *a);

which is certainly not what you meant. You meant to say pass the vector by
reference, or pass it using *, not both.
my ask is: how the vector are passed to functions?

thanx alot

Giulio

Not quite sure why your compiler wouldn't let you point to the vector, it is
perfectly legal:

void f(const std::vector<int> *a);

but what you probably wanted was a constant reference to the vector:

void f(const std::vector<int> &a);

which allows you not to copy the vector, and ensure it doesn't change.
Moreover, it is much faster, as you wanted. And, unlike pointers, it doesn't
deal with mucking about with dereferencing, etc.

-- MiniDisc_2k2
To reply, replace nospam.com with cox dot net.
 
A

Andrew Heath

Giulio said:
passing simply the vectors I need it makes me an error in the while
argument.
how to solve this?

thanx
Giulio
---------------------------------
std::vector<tipi::prezzo>::iterator pr = prezzo_.begin();
std::vector<tipi::eek:perazione>::iterator op = op_.begin();
std::vector<tipi::prezzo>::iterator finePr = prezzo_.end();
std::vector<tipi::eek:perazione>::iterator fineOp = op_.end();

while( op!=finePr && pr!=fineOp ) {


I think what you mean is:

while( op!=fineOp && pr!=finePr ) {
 
J

Jerry Coffin

"Giulio" said:
I'm doing a class who takes in constructor a vector<int> keeps and makes
some statistic on the vector whitout modifiing it.

I would not pass the vector itself at all. Instead, I'd consider
passing a pair of iterators, one to the beginning and the other to the
end of the vector.
 
G

Giulio

Instead, I'd consider
passing a pair of iterators, one to the beginning and the other to the
end of the vector.
wow this is a great idea...

thanx to all for the good answers
Giulio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top