Vector of Vectors

This is a discussion on Vector of Vectors within the C++ Programming forums, part of the General Programming Boards category; How do you traverse through double vectors. Code: for(int l = 0; l < myWordsInLetter->size(); l++) { for(int m = ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    Vector of Vectors

    How do you traverse through double vectors.

    Code:
    for(int l = 0; l < myWordsInLetter->size(); l++)
    {
    	for(int m = 0; m < myWordsInLetter->at(l).size(); m++)
    	{
    		cout << myWordsInLetter->at(l).at(m);
    	}
    	cout << " ";
    }
    where myWordsInLetter is a vector<vector<string> >

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just like that, I suppose. Except if myWordsInLetter is a vector<vector<string> >, you wouldn't ever use ->, but always .

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    30
    It gives me an

    terminate called after throwing an instance of 'std:ut_of_range'
    what(): vector::_M_range_check
    Aborted

    when I run it and it is a pointer to that vector

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    30
    Wait here is the problem how do I do this right

    Code:
    ifstream ifstr(myInputFile.c_str());
    	if(!ifstr)
    	{
    		cerr << "Cannot open: " << myConversionTableFile << "." << endl;
    		exit(ERROR);	    
    	}
    
    	string tempstr;
    	int i = 0;
    	while(ifstr >> tempstr)
    	{
    		for(int k = 0; k < tempstr.size(); k++)
    		{
    			myWordsInLetter->at(i).push_back(tempstr);
    		}
    		i++;
    	}

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Code:
    myWordsInLetter->push_back(vector<string>())
    Do that before the inner loop.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I normally use iterators...
    Code:
    int main() {
      typedef std::vector<std::string> vs;
      typedef std::vector<vs> vvs;
    
      vvs *myWordsInLetter;
      // Fill point pointer to instance
      
      for ( vvs::iterator outer_iter=myWordsInLetter->begin(); outer_iter!=myWordsInLetter->end(); outer_iter++ ) {
        for ( vs::iterator inner_iter=outer_iter->begin(); inner_iter!=outer_iter->end(); inner_iter++ ) {
          // do whatever you want with inner iter
          std::cout<< *inner_iter << "\n";
        }
      }
    
      return 0;
    }
    Well, the outer vector needs to be pushed back too. So using ->at(i) is trying to access memory that's not there. So you're going to have to probably make a temp vector<string> which is filled in the inner for loop, then outside of that you .push_back( temp_vector ).

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Yes, and this syntax is so much longer and it's so much more unsafe, because dereferencing an invalid iterator is undefined, while using .at is defined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 03:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 12:13 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

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