| | |
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:
Solved Threads: 0
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

i
you mean a vector like below? There are probably other ways too, but this is how I do it.
c Syntax (Toggle Plain Text)
vector< vector< string> > theList; vector<string>::iterator it; vector<string>& innerList = theList[0]; it = innerList.begin(); for( ; it != innerList.end(); it++) { // blabla }
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.
>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:
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)
#include <iostream> #include <iomanip> #include <vector> int main() { std::vector< std::vector<int> > v; for ( int i = 0; i < 10; i++ ) { v.push_back ( std::vector<int>() ); for ( int j = 0; j < 10; j++ ) v[i].push_back ( i + j ); } std::vector< std::vector<int> >::iterator row_it = v.begin(); std::vector< std::vector<int> >::iterator row_end = v.end(); for ( ; row_it != row_end; ++row_it ) { std::vector<int>::iterator col_it = row_it->begin(); std::vector<int>::iterator col_end = row_it->end(); for ( ; col_it != col_end; ++col_it ) std::cout<< std::setw ( 3 ) << *col_it; std::cout<<'\n'; } }
I'm here to prove you wrong.
![]() |
Similar Threads
- vector iterators vs operator [] (C++)
- Accessing data inside a vector (C++)
- Need help writing my own reverse iterator class (C++)
- Vector hell (C)
- stl vector - can you delete by position? (C++)
- 1 cin statement/multiple values to a vector (C++)
- How to overload subscript operator? (C)
Other Threads in the C++ Forum
- Previous Thread: c++ Timer?
- Next Thread: Help with 1.pointers and 2.error checking
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






