Friday 24 November 2017

C++ Program to Implement Multimap in STL


Code:

#include     iostream
#include     map
#include     string
using namespace std;
int main ()
{
    multimap mymm;
    multimap::iterator it;
    mymm.insert (pair('x', 100));
    mymm.insert (pair('y', 200));
    mymm.insert (pair('y', 300));
    mymm.insert (pair('y', 400));
    mymm.insert (pair('z', 500));
    mymm.insert (pair('z', 500));
    cout<<"Size of the multimap: "<< mymm.size() <
    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::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: "<
    // showing contents:
    cout << "Multimap contains:\n";
    for (it = mymm.begin(); it != mymm.end(); ++it)
        cout << (*it).first << " => " << (*it).second << '\n';
    return 0;
}



Output:

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



More C++ Programs:
















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...