944,095 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 9883
  • C++ RSS
Feb 15th, 2007
0

how can i use an iterator for a 2d vector?

Expand Post »
I have read that iterators are used to traverse through the elements of container classes. If I wanted to use iterators to move through elements of a 2d vector, how am I going to declare it and use it? Maybe what I said was not too clear, let me rephrase it to be : " how can I iterate through a 2d vector?"


i
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tinie is offline Offline
6 posts
since Feb 2007
Feb 15th, 2007
0

Re: how can i use an iterator for a 2d vector?

you mean a vector like below? There are probably other ways too, but this is how I do it.
  1. vector< vector< string> > theList;
  2. vector<string>::iterator it;
  3.  
  4. vector<string>& innerList = theList[0];
  5. it = innerList.begin();
  6. for( ; it != innerList.end(); it++)
  7. {
  8. // blabla
  9. }
Last edited by Ancient Dragon; Feb 15th, 2007 at 12:40 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,960 posts
since Aug 2005
Feb 15th, 2007
0

Re: how can i use an iterator for a 2d vector?

Ancient Dragon's example will work for the first elemend of the 2D vector, and you'll probably have to nest it to go through all the rows of the 2D vector.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 17th, 2007
0

Re: how can i use an iterator for a 2d vector?

Do i need to declare another iterator to traverse the rows and another one to traverse each elements? Also, can you show how to do the nesting, since I'm a bit confused?
Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tinie is offline Offline
6 posts
since Feb 2007
Feb 17th, 2007
1

Re: how can i use an iterator for a 2d vector?

>Do i need to declare another iterator to traverse the rows and another one to traverse each elements?
Yes. Each row is an independent data structure, so you have no choice but to use two iterators. It's exactly the same as when you build the vector of vectors:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4.  
  5. int main()
  6. {
  7. std::vector< std::vector<int> > v;
  8.  
  9. for ( int i = 0; i < 10; i++ ) {
  10. v.push_back ( std::vector<int>() );
  11.  
  12. for ( int j = 0; j < 10; j++ )
  13. v[i].push_back ( i + j );
  14. }
  15.  
  16. std::vector< std::vector<int> >::iterator row_it = v.begin();
  17. std::vector< std::vector<int> >::iterator row_end = v.end();
  18.  
  19. for ( ; row_it != row_end; ++row_it ) {
  20. std::vector<int>::iterator col_it = row_it->begin();
  21. std::vector<int>::iterator col_end = row_it->end();
  22.  
  23. for ( ; col_it != col_end; ++col_it )
  24. std::cout<< std::setw ( 3 ) << *col_it;
  25. std::cout<<'\n';
  26. }
  27. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ Timer?
Next Thread in C++ Forum Timeline: Help with 1.pointers and 2.error checking





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC