PDA

View Full Version : vector of vector size



mickey
12th February 2007, 21:39
hi, I need to retrieve this (but i wouldn't like use 'i'):


vector < vector <double> >* e
e->at(i).size()
e->at(0).size() //it's the same

In this case the vector contains n vectors which must have the same size!
so e->at(0).size() and e->at(1).size() and e->at(n).size() are the same value;
But I don't like use (0) the reach the size.

How can I do?
and: maybe it isn't necessary use a vector of vector !?

thanks

wysota
12th February 2007, 22:41
All depends what you need... Why do you keep a pointer to the vector instead of referencing it by value?

Anyway:

double val = (*e)[i][0];

mickey
13th February 2007, 15:22
Why do you keep a pointer to the vector instead of referencing it by value?


Net::Net(vector < vector <double> >* e)

if I refer it in the function by value, isn't create a copy inside that memeber? Inside it I only retrieve the values of 'e', so I think pointer or value is the same!? (but pointer avoid me create an unuseful copy of 'e' !??!).
thanks

wysota
13th February 2007, 15:38
You can always pass a reference to avoid making copies.


void someFunc(const vector<int> &myvec){
// no copy here
printf("VECTOR HAS %d ELEMENTS\n", myvec.size());
}

mickey
13th February 2007, 15:51
yes, Does 'pointer' and 'reference' reach the same goal? (in this case). is there one strong reason to use 'reference' in this case?

wysota
13th February 2007, 15:59
yes, Does 'pointer' and 'reference' reach the same goal? (in this case).
In this case yes.


is there one strong reason to use 'reference' in this case?

References are easier to handle and you can't have "dangling references" in contrast to "dangling pointers" (meaning you wouldn't get the segmentation fault you got). Some people even say that Using Pointers In C++ Is Bad (C).