C++ STL  iterator, traverse the multiset 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 multiset container in C++ programming

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

 

// C++ STL iterator, multiset example

#include <iostream>

#include <set>

using namespace std;

 

int main(void)

{

// multiset container of int data type

multiset<int> tst;

 

// insert elements

tst.insert(12);

tst.insert(21);

tst.insert(32);

tst.insert(31);

tst.insert(9);

tst.insert(14);

tst.insert(21);

tst.insert(31);

tst.insert(7);

 

// iterate over all elements and print, separated by space

multiset<int>::const_iterator pos;

cout<<"multiset: ";

// pre-increment and pre-decrement are faster than post-increment and post-decrement...

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

cout<<*pos<<' ';

cout<<endl;

return 0;

}

 

Output examples:

 

multiset: 7 9 12 14 21 21 31 31 32

Press any key to continue . . .

 

 

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