vector<vector<int>>

This is a discussion on vector<vector<int>> within the C++ Programming forums, part of the General Programming Boards category; Hi, Let say I declared something like: vector<vector<int>> r How would I set the values for r and display it ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    51

    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
    Which is the master, which is the student?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    51
    thanks SilentStrike,

    I get it now ...
    Which is the master, which is the student?

Popular pages Recent additions subscribe to a feed

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