This C++ Program demonstrates the implementation of Multimap in STL.
Here is source code of the C++ Program to implement Multimap in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Implement Multimap in STL
*/
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main ()
{
multimap<char, int> mymm;
multimap<char, int>::iterator it;
mymm.insert (pair<char, int>('x', 100));
mymm.insert (pair<char, int>('y', 200));
mymm.insert (pair<char, int>('y', 300));
mymm.insert (pair<char, int>('y', 400));
mymm.insert (pair<char, int>('z', 500));
mymm.insert (pair<char, int>('z', 500));
cout<<"Size of the multimap: "<< mymm.size() <<endl;
cout << "Multimap contains:\n";
for (it = mymm.begin(); it != mymm.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
for (char c = 'x'; c <= 'z'; c++)
{
cout << "There are " << mymm.count(c) << " elements with key " << c << ":";
multimap<char, int>::iterator it;
for (it = mymm.equal_range(c).first; it != mymm.equal_range(c).second; ++it)
cout << ' ' << (*it).second;
cout << endl;
}
it = mymm.find('x');
mymm.erase (it);
cout<<"Size of the multimap: "<<mymm.size()<<endl;
// showing contents:
cout << "Multimap contains:\n";
for (it = mymm.begin(); it != mymm.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
return 0;
}
$ g++ multimap_stl.cpp $ a.out Size of the multimap: 6 Multimap contains: x => 100 y => 200 y => 300 y => 400 z => 500 z => 500 There are 1 elements with key x: 100 There are 3 elements with key y: 200 300 400 There are 2 elements with key z: 500 500 Size of the multimap: 5 Multimap contains: y => 200 y => 300 y => 400 z => 500 z => 500 ------------------ (program exited with code: 1) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Check Programming Books
- Practice Design & Analysis of Algorithms MCQ
- Check Computer Science Books
- Practice Computer Science MCQs