Vectors- Basic Doubt

Jan 22, 2013 at 8:24pm
I am very new to vectors and would like know how assignment works in case of a vector of vectors [2-D Vector].
I want to input a list of elements(from stdin) to the first row of the vector of vectors. How should I go about it?
I tried to assign it as-
1
2
3
4
 for(loopVar1=0;loopVar1<sizeOfSequence;loopVar1++) {
            cin>>element;
            vector1[0][loopVar1]=element;
     }


I realize that this is naive and is throwing a seg fault. I know how .push_back works in case of 1-D vectors but am not sure what to do here. Please help.
Jan 22, 2013 at 8:33pm
1
2
3
4
 for(loopVar1=0;loopVar1<sizeOfSequence;loopVar1++) {
            cin>>element;
            vector1[0].push_back(element);
     }
Jan 22, 2013 at 8:35pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<std::vector<int>> v;

v.push_back( std::vector<int>() );

const int N = 10;

v[0].reserve( N );
 
for ( int i = 0; i < N; i++ )
{
   std::cout << "Enter a number: ";
   int x;
   std::cin >> x;
   v[0].push_back( x );
}
Jan 22, 2013 at 9:11pm
Thank you.
Topic archived. No new replies allowed.