Vector Of Vectors

Jan 25, 2011 at 3:06am
closed account (zb0S216C)
OK, I've came across a vector of vectors defined like this:
vector< vector< int > > VecOfVec;

Does this act in the same way as this:
int 2DArray[ 2 ][ 2 ];?

Here's what I know of them already, tell me if I'm wrong.

To add to the base vector, I need to push back another vector, like this:
1
2
vector< int > AddThis( 1, 2 );
VecOfVec.push_back( AddThis );

To access the elements( VecOfVec -> Vector -> Element ), I do this:
 
VecOfVect[ 0 ][ 0 ] = 3;


Have I understood this correctly?
Last edited on Jan 25, 2011 at 3:07am
Jan 25, 2011 at 3:50am
That looks right to me.
Jan 25, 2011 at 3:54am
Personally for me, using array subscript to access vector element is risky to me. What if you passed in some invalid index? You will get error definitely.

So my preferred approach in using vector is to use push_back method. The code maybe longer but at least I don't have to worry about invalid index issues as vector internal implementation will process accordingly.
Jan 25, 2011 at 3:56am
You mean .at(), right?
Jan 25, 2011 at 7:29am
closed account (zb0S216C)
You make a good point, sohguanh. I'll use the .at( ); member in the future.
Topic archived. No new replies allowed.