C++ examples for STL:iterator
Iterating map, vector
#include <iostream> #include <string> #include <map> #include <vector> using namespace std; int main() //w ww . j a v a2 s .c o m { // Iterating through a map map<string, int> NumberWords; NumberWords["ten"] = 10; NumberWords["twenty"] = 20; NumberWords["thirty"] = 30; map<string, int>::iterator loopy = NumberWords.begin(); while (loopy != NumberWords.end()) { cout << loopy->first << " "; cout << loopy->second << endl; loopy++; } // Iterating through a vector vector<string> Words; Words.push_back("hello"); Words.push_back("there"); Words.push_back("ladies"); Words.push_back("and"); Words.push_back("aliens"); vector<string>::iterator vectorloop = Words.begin(); while (vectorloop != Words.end()) { cout << *vectorloop << endl; vectorloop++; } return 0; }