okay so I can make 2d arrays easy if I know how many items I am going to have to start with but in my main project they are dynamically created so I can not do that and I need to make a 2d QList which I can not figure out how to do I have been tyring for like an hour now.
here is an example with just a normal 2d array when I know the given values.
int numbers[4][2] ={
{2, 4},
{3, 6},
{5, 10},
{8, 16}
};
for(int i = 0; i<4; i++){
for(int j = 0; j<2; j++){
cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
}
cout << endl;
}
int numbers[4][2] ={
{2, 4},
{3, 6},
{5, 10},
{8, 16}
};
for(int i = 0; i<4; i++){
for(int j = 0; j<2; j++){
cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
}
cout << endl;
}
To copy to clipboard, switch view to plain text mode
the output is:
numbers[0][0]: 2 numbers[0][1]: 4
numbers[1][0]: 3 numbers[1][1]: 6
numbers[2][0]: 5 numbers[2][1]: 10
numbers[3][0]: 8 numbers[3][1]: 16
now here is what I am trying to do which does not work:
QList<int> number;
QList<QList<int> > numberList;
number << 2 << 3 << 5 << 8;
numberList << number;
number[0] += 2;
number[1] += 3;
number[2] += 5;
number[3] += 8;
numberList << number;
for(int i = 0; i<numberList.size(); i++){
for(int j = 0; j<number.size(); j++){
// cout << number[j] << endl;
cout << "numberList["<<i<<"]["<<j<<"]: " << numberList[i][j] << endl;
}
}
QList<int> number;
QList<QList<int> > numberList;
number << 2 << 3 << 5 << 8;
numberList << number;
number[0] += 2;
number[1] += 3;
number[2] += 5;
number[3] += 8;
numberList << number;
for(int i = 0; i<numberList.size(); i++){
for(int j = 0; j<number.size(); j++){
// cout << number[j] << endl;
cout << "numberList["<<i<<"]["<<j<<"]: " << numberList[i][j] << endl;
}
}
To copy to clipboard, switch view to plain text mode
I tried several ways but just cant seem to figure out making a 2d QList of ints =/
any suggestions/help/explainations would be greatly appreciated.
thanks.
Added after 46 minutes:
okay so I got it working somewhat by using a vector but I do not understand the code 100% from -- http://www.daniweb.com/software-deve...8527/2d-vector
vector<vector<int> > numbers;
int k[4] = {2, 3, 5, 8};
for ( int i = 0; i < 4; i++ ) {
numbers.push_back (vector<int>());
for ( int j = 0; j < 2; j++ ){
numbers[i].push_back (k[i]*(j+1));
}
}
for ( int i = 0; i < 4; i++ ) {
for ( int j = 0; j < 2; j++ ){
cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
}
cout << endl;
}
vector<vector<int> > numbers;
int k[4] = {2, 3, 5, 8};
for ( int i = 0; i < 4; i++ ) {
numbers.push_back (vector<int>());
for ( int j = 0; j < 2; j++ ){
numbers[i].push_back (k[i]*(j+1));
}
}
for ( int i = 0; i < 4; i++ ) {
for ( int j = 0; j < 2; j++ ){
cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
}
cout << endl;
}
To copy to clipboard, switch view to plain text mode
the output is:
numbers[0][0]: 2 numbers[0][1]: 4
numbers[1][0]: 3 numbers[1][1]: 6
numbers[2][0]: 5 numbers[2][1]: 10
numbers[3][0]: 8 numbers[3][1]: 16
I have not used vectors before and when I looked up the reference for "push_back" it says it is the same as append I believe. but how come I can not use numbers.append or numbers << instead of numbers.push_back? is there a difference? it doesnt really have any info on push_back here http://qt-project.org/doc/qt-4.8/qvector.html#push_back
any info/advice on vectors would be appreciated.
Bookmarks