someone had posted about creating a 2d array using a vector of vectors. i tried something just so i could see how it was internally organized but the results are not what i expect. I realize using the subscript operator isnt safe but i didnt know how to do it otherwise. what im confused about is i thought i used every valid way without going out of bounds yet 2 nujmbers are never printed. it makes no sense to me.
#include <iostream>
#include <vector>
// only open the namespace names we intend to use
using std::cout; using std::endl;
using std::vector;
int main(int argc , char** argv)
{
vector< vector< int > >mymatrix;
vector< int >AddThis(4,7);
vector< int >AddThat(5,3);
mymatrix.push_back(AddThis);
mymatrix.push_back(AddThat);
cout << mymatrix[0][0] << endl;
cout << mymatrix[1][1] << endl;
cout << mymatrix[1][0] << endl;
cout << mymatrix[0][1] << endl;
}
Perhaps you have the wrong idea about what that vector constructor does? vector< int >AddThis(4,7); creates a vector with 4 elements that are all initialized to 7.
std::vector's constructor doesn't do what you're thinking. When you say std::vector<T>(x, y), you're saying "make me a vector of x elements and initialize them all with the value y".