Jump to content

  • Log In with Google      Sign In   
  • Create Account


Traversing a List of Vectors


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Chad Smith   Members   -  Reputation: 1102

Like
0Likes
Like

Posted 06 April 2013 - 01:19 PM

in C++ lets say I have a list of vectors like:

 

std::list<std::vector<int>> myList;

is their any quick way or what would the best way to traverse through that vector in the list?  Meaning is their any good way to get every number in that vector, then move onto the next node in the list and getting all of those ints, so on and so on.

 

I know how to traverse the list on it's on though I'm unsure how to get all the data in that vector for that current list node.

 

Thanks for any help!!



Sponsor:

#2 ultramailman   Prime Members   -  Reputation: 1563

Like
0Likes
Like

Posted 06 April 2013 - 01:23 PM

Well, normally, wouldn't you just use an iterator in a for loop to loop through the list, and get data from the current node? How are you doing it? Are you unsatisfied with the way you do it?

 

edit:

Ohhh, do you mean iterating through every int that is in those vectors that are in those lists? You could use another for loop to iterate the vector, like you would if you were to iterate through a 2d array.

 

Or you could write an iterator class for list<vector>, but that might be overkill unless it's used very often.


Edited by ultramailman, 06 April 2013 - 01:30 PM.


#3 EarthBanana   Members   -  Reputation: 874

Like
2Likes
Like

Posted 06 April 2013 - 01:44 PM

    std::list<std::vector<int>> intVecList;
    std::list<std::vector<int>>::iterator listIter = intVecList.begin();
    while (listIter != intVecList.end())
    {
        std::vector<int>::iterator vecIter = listIter->begin();
        while (vecIter != listIter->end())
        {
            *vecIter += 5; // add 5 to the current int
            ++vecIter;
        }
        ++listIter;
    }

 

This is a good way to do it..



#4 Chad Smith   Members   -  Reputation: 1102

Like
0Likes
Like

Posted 06 April 2013 - 01:54 PM

    std::list<std::vector<int>> intVecList;
    std::list<std::vector<int>>::iterator listIter = intVecList.begin();
    while (listIter != intVecList.end())
    {
        std::vector<int>::iterator vecIter = listIter->begin();
        while (vecIter != listIter->end())
        {
            *vecIter += 5; // add 5 to the current int
            ++vecIter;
        }
        ++listIter;
    }

 

This is a good way to do it..

Thanks!  That should work for what I need it for!  Not sure why I didn't think of that.  Thanks again.



#5 nimrodson   Members   -  Reputation: 275

Like
2Likes
Like

Posted 06 April 2013 - 02:13 PM

If you're using C++11, you can try this too:

 

#include <vector>
#include <list>
#include <iostream>

int main()
{
  std::list<std::vector<int>> listVectors{{1,2,3},{4,5,6},{7,8,9}};
  for ( auto& vec : listVectors )
    for ( int& i : vec )
      //process each int i
      std::cout << i << std::endl;
}



#6 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
0Likes
Like

Posted 06 April 2013 - 02:42 PM

Yep, the C++11 version with range-for and auto is much better if your compiler supports those things.

 

Which compiler (exact version) are you using Chad?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS