C++ STL  iterator, traverse the multimap container with begin() and end() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the begin() and end() iterator to traverse the multimap container in C++ programming

To show: How to traverse the multimap container in C++ programming using the begin() and end() iterator

 

// C++ STL iterator, multimap simple example

#include <iostream>

#include <map>

#include <string>

using namespace std;

 

int main(void)

{

// type of the collections

multimap<int, string> mmp;

 

// set container for int/string values insert some elements in arbitrary order notice a value of key 1

mmp.insert(make_pair(5,"learn"));

mmp.insert(make_pair(2,"map"));

mmp.insert(make_pair(1,"Testing"));

mmp.insert(make_pair(7,"tagged"));

mmp.insert(make_pair(4,"strings"));

mmp.insert(make_pair(6,"iterator!"));

mmp.insert(make_pair(1,"the"));

mmp.insert(make_pair(3,"tagged"));

 

// iterate over all elements and print, element member second is the value

//

// multimap iterator

multimap<int, string>::iterator pos;

// print the data

for(pos = mmp.begin(); pos != mmp.end(); ++pos)

cout<<pos->second<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

Testing the map tagged strings learn iterator! tagged

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index