Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   STL passing vector fastly (http://www.velocityreviews.com/forums/t267899-stl-passing-vector-fastly.html)

Giulio 06-30-2003 11:40 PM

STL passing vector fastly
 
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



Giulio 06-30-2003 11:48 PM

Re: STL passing vector fastly
 
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::operazione>::iterator op = op_.begin();
std::vector<tipi::prezzo>::iterator finePr = prezzo_.end();
std::vector<tipi::operazione>::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
-----------------------------------



David White 07-01-2003 12:15 AM

Re: STL passing vector fastly
 
Giulio <giulio.gL E V A@email.it> wrote in message
news:oU3Ma.4237$FI4.113215@tornado.fastwebnet.it.. .
> 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




David White 07-01-2003 12:19 AM

Re: STL passing vector fastly
 

Giulio <giulio.gL E V A@email.it> wrote in message
news:X%3Ma.4240$FI4.113145@tornado.fastwebnet.it.. .
> 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::operazione>::iterator op = op_.begin();
> std::vector<tipi::prezzo>::iterator finePr = prezzo_.end();
> std::vector<tipi::operazione>::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::operazione, prezzo_ and op_ and it
might be easier to see what is wrong.

David




Mike Wahler 07-01-2003 12:32 AM

Re: STL passing vector fastly
 

Giulio <giulio.gL E V A@email.it> wrote in message
news:oU3Ma.4237$FI4.113215@tornado.fastwebnet.it.. .
> 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:
const std::vector<int>& v;
};

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


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

-Mike




MiniDisc_2k2 07-01-2003 02:31 AM

Re: STL passing vector fastly
 

"Giulio" <giulio.gL E V A@email.it> wrote in message
news:oU3Ma.4237$FI4.113215@tornado.fastwebnet.it.. .
> 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.



Andrew Heath 07-01-2003 03:48 AM

Re: STL passing vector fastly
 
Giulio wrote:

> 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::operazione>::iterator op = op_.begin();
> std::vector<tipi::prezzo>::iterator finePr = prezzo_.end();
> std::vector<tipi::operazione>::iterator fineOp = op_.end();
>
> while( op!=finePr && pr!=fineOp ) {



I think what you mean is:

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


Jerry Coffin 07-01-2003 12:36 PM

Re: STL passing vector fastly
 
In article <oU3Ma.4237$FI4.113215@tornado.fastwebnet.it>, "Giulio"
<giulio.gL E V A@email.it> says...
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Giulio 07-01-2003 11:33 PM

Re: STL passing vector fastly
 
> 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




All times are GMT. The time now is 10:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.