push back in 2D vectors ?

Hello I am trying to use push back in a 2D vector but I don't know how to. This is what I have:
1
2
3
4
5
6
7
vector <vector <BigInt> > matr;

for (BigInt i=0;i<rij;i++) {
	for (BigInt j=0;j<kolom-1;j++) {
                matr.push_back().push_back((i+1)^(pow-j));
	}
}

I quickly invented something but that doesn't work obviously. it should be equivalent to this: (the only problem in the code below is that those indexes don't exist yet that's why I need push_back())
1
2
3
4
5
for (BigInt i=0;i<rij;i++) {
	for (BigInt j=0;j<kolom-1;j++) {
               matr[int(i)][int(j)]=(i+1)^(pow-j);
	}
}
Last edited on
Error is here:
matr.push_back().push_back((i+1)^(pow-j));
is a method not a class.

The simples option is:
1
2
3
4
5
6
7
vector <vector <int> > matr;
//for
//{
vector <int> newColumn;
matr.push_back(newColumn);
matr.at(rowNumber).push_back(intValue);
//} 

then:
matr.at(columns).at(rows) = 2;

You can also create vector of vector<int> pointers:
1
2
3
4
vector < vector <int> * > matr;

matr.push_back(new vector <int>);
matr.at(0)->push_back(5);

then you would use:
matr.at(columns)->at(rows) = 2
But in case of dynamic allocations you would also need to take care of deleting allocated rows.

Another solution would be using some c++ matrix library. I bet boost has one.

edit.: similar problem was discussed here :
http://www.cplusplus.com/forum/beginner/12409/
Last edited on
Thank you
Topic archived. No new replies allowed.