Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL passing vector fastly

Reply
Thread Tools

STL passing vector fastly

 
 
Giulio
Guest
Posts: n/a
 
      06-30-2003
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


 
Reply With Quote
 
 
 
 
Giulio
Guest
Posts: n/a
 
      06-30-2003
passing simply the vectors I need it makes me an error in the while
argument.
how to solve this?

thanx
Giulio
---------------------------------
std::vector<tipi:rezzo>::iterator pr = prezzo_.begin();
std::vector<tipi:perazione>::iterator op = op_.begin();
std::vector<tipi:rezzo>::iterator finePr = prezzo_.end();
std::vector<tipi: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
-----------------------------------


 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      07-01-2003
Giulio <giulio.gL E V http://www.velocityreviews.com/forums/(E-Mail Removed)> wrote in message
newsU3Ma.4237$(E-Mail Removed).. .
> 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



 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      07-01-2003

Giulio <giulio.gL E V (E-Mail Removed)> wrote in message
news:X%3Ma.4240$(E-Mail Removed).. .
> passing simply the vectors I need it makes me an error in the while
> argument.
> how to solve this?
>
> thanx
> Giulio
> ---------------------------------
> std::vector<tipi:rezzo>::iterator pr = prezzo_.begin();
> std::vector<tipi:perazione>::iterator op = op_.begin();
> std::vector<tipi:rezzo>::iterator finePr = prezzo_.end();
> std::vector<tipi: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:rezzo, tipi:perazione, prezzo_ and op_ and it
might be easier to see what is wrong.

David



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      07-01-2003

Giulio <giulio.gL E V (E-Mail Removed)> wrote in message
newsU3Ma.4237$(E-Mail Removed).. .
> 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



 
Reply With Quote
 
MiniDisc_2k2
Guest
Posts: n/a
 
      07-01-2003

"Giulio" <giulio.gL E V (E-Mail Removed)> wrote in message
newsU3Ma.4237$(E-Mail Removed).. .
> 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.


 
Reply With Quote
 
Andrew Heath
Guest
Posts: n/a
 
      07-01-2003
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:rezzo>::iterator pr = prezzo_.begin();
> std::vector<tipi:perazione>::iterator op = op_.begin();
> std::vector<tipi:rezzo>::iterator finePr = prezzo_.end();
> std::vector<tipi:perazione>::iterator fineOp = op_.end();
>
> while( op!=finePr && pr!=fineOp ) {



I think what you mean is:

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

 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      07-01-2003
In article <oU3Ma.4237$(E-Mail Removed)>, "Giulio"
<giulio.gL E V (E-Mail Removed)> 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.
 
Reply With Quote
 
Giulio
Guest
Posts: n/a
 
      07-01-2003
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TXD and RXD leds of cisco 837 blinking fastly thiakass Hardware 0 10-14-2010 10:22 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments