# vector<vector<int>>

• 05-05-2002
Jez_Master
vector<vector<int>>
Hi,

Let say I declared something like:

vector<vector<int>> r

How would I set the values for r and display it out to the screen?

Thx

I've tried r[1[1]]..but it doesn't work :D
• 05-05-2002
SilentStrike
Here is some code demonstrating how to do it. I'd recommend either creating a matrix class yourself, (or using mine :) ), rather than using nested vectors, because you will simply end up with a lot of redudancy after you do it awhile. You can comment out of all the stuff referring to Matrix if you don't want to use it and still compile it. If you want to use the Matrix class, it's here.. and you can use it without the GPLing your work if you so choose as well.

You'll need to get rid of the line "#include "../Config.h" in order for the matrix to compile, however.

http://cvs.sourceforge.net/cgi-bin/v...viewcvs-markup

Code:

```#include <iostream> #include <vector> #include "Matrix.h" using namespace std; // ugly int main() {         // demonstrate vector<vector<stuff> > method         {                 cout << "Using vector of vectors as a matrix " << endl;                 vector<vector<int> > fakeMat;                 const int NUM_ROWS=10;                 const int NUM_COLS=5;                 fakeMat.resize(NUM_ROWS);                 for (int i = 0; i < NUM_ROWS; i++) {                         fakeMat[i].resize(NUM_COLS);                         for (int j = 0; j < NUM_COLS; j++) {                                 fakeMat[i][j] = i*j;                         }                 }                 for (int row = 0; row < NUM_ROWS; row++) {                         for (int col = 0; col < NUM_COLS; col++) {                                 cout << fakeMat[row][col] << "\t";                         }                         cout << endl;                 }         }         // demonstrate easier way with my matrix class         {                 cout << "Using matrix class as matrix " << endl;                 Util::Matrix<int> myMat(10, 5, 0); // 10 rows, 5 cols, all of which are 0                 for (int i = 0; i < myMat.numRows(); i++) {                         for (int j = 0; j < myMat.numCols(); j++) {                                 myMat(i,j) = i*j;                         }                 }                 for (int row = 0; row < myMat.numRows(); row++) {                         for (int col=0; col < myMat.numCols(); col++) {                                 cout << myMat(row,col) << "\t";                         }                         cout << endl;                 }         }         return 0; }```
• 05-05-2002
Jez_Master
thanks SilentStrike,

I get it now :)...