vector of vectors

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#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".

EDIT: too slow.
Last edited on
haha yea i was. ok thanks. ill go put my dunce hat on now. ill leave my code up so everyone can laugh at me.
Last edited on
Topic archived. No new replies allowed.