vector<vector<int> > access element.

This is a discussion on vector<vector<int> > access element. within the C++ Programming forums, part of the General Programming Boards category; I have a vector of vector that is populated as follow: Code: typedef std::vector< std::vector<int> > matrix; int main() { ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452

    vector<vector<int> > access element.

    I have a vector of vector that is populated as follow:
    Code:
    typedef std::vector< std::vector<int> > matrix;
    
    int main()
    {
      using namespace std; 
      matrix mat;
      vector<int> vec;
      for(int i = 0 ; i < 10 ; ++i){
        for( int j = 0 ; j < 10 ; j++){
          vec.push_back(i+j);
          }
        mat.push_back(vec);
      }
    }
    I thought that this would print out the matrix:
    Code:
    // version 1:
        for(matrix::iterator it = mat.begin() ; it != mat.end() ; ++it)
          {
    	copy(it->begin(),it->end(),ostream_iterator<int> (cout,""));
    	cout << endl; 
          }
    
    // version 2: 
      for(matrix::iterator it = mat.begin() ; it != mat.end() ; it++)
          {
    	for(vector<int>::iterator it2 = it->begin() ; it2 != it->end() ;++it2)
    	  cout << *it2;
    	cout << endl; 
          }
    Using either version above, I got this instead of the matrix
    Code:
    0123456789
    012345678912345678910
    012345678912345678910234567891011
    0123456789123456789102345678910113456789101112
    012345678912345678910234567891011345678910111245678910111213
    012345678912345678910234567891011345678910111245678910111213567891011121314
    0123456789123456789102345678910113456789101112456789101112135678910111213146789101112131415
    012345678912345678910234567891011345678910111245678910111213567891011121314678910111213141578910111213141516
    012345678912345678910234567891011345678910111245678910111213567891011121314678910111213141578910111213141516891011121314151617
    0123456789123456789102345678910113456789101112456789101112135678910111213146789101112131415789101112131415168910111213141516179101112131415161718
    how should I access element of the vectors?

    EDIT:: Nevermind,I found my mistake; i forgot to clear the "vec" when populating the matrix.
    Code:
     for(int i = 0 ; i < 10 ; ++i){
        for( int j = 0 ; j < 10 ; j++){
          vec.push_back(i+j);
          }
        mat.push_back(vec);
        vec.clear(); <--- forgot this line ;(
      }
    Last edited by nimitzhunter; 01-23-2011 at 04:21 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FREAD error
    By xphoenix in forum C Programming
    Replies: 4
    Last Post: 07-05-2010, 08:47 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. runtime error with vector
    By te5la in forum C++ Programming
    Replies: 26
    Last Post: 07-17-2008, 08:57 AM
  4. Replies: 3
    Last Post: 03-22-2005, 02:27 AM
  5. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM

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