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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2007
Posts: 6
Reputation: tinie is an unknown quantity at this point 
Solved Threads: 0
tinie tinie is offline Offline
Newbie Poster

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

 
0
  #1
Feb 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

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

 
0
  #2
Feb 15th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

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

 
0
  #3
Feb 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 6
Reputation: tinie is an unknown quantity at this point 
Solved Threads: 0
tinie tinie is offline Offline
Newbie Poster

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

 
0
  #4
Feb 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #5
Feb 17th, 2007
>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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC