Multimap vs Map in C++ STL with Examples

23 Nov, 2021
Map in C++ STL
Map stores unique key-value pairs in a sorted manner. Each key is uniquely associated with a value that may or may not be unique. A key can be inserted or deleted from a map but cannot be modified. Values assigned to keys can be changed. It is a great way for quickly accessing value using the key and it is done in O(1) time. 
C++
#include <iostream>#include <iterator>#include <map>  using namespace std;  int main(){      // empty map container    map<int, int> gquiz1;      // insert elements in random order    gquiz1.insert(pair<int, int>(1, 40));    gquiz1.insert(pair<int, int>(2, 30));    gquiz1.insert(pair<int, int>(3, 60));    gquiz1.insert(pair<int, int>(4, 20));    gquiz1.insert(pair<int, int>(5, 50));    gquiz1.insert(pair<int, int>(6, 50));    gquiz1.insert(pair<int, int>(7, 10));      // printing map gquiz1    map<int, int>::iterator itr;    cout << "\nThe map gquiz1 is : \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }    cout << endl;      // assigning the elements from gquiz1 to gquiz2    map<int, int> gquiz2(gquiz1.begin(), gquiz1.end());      // print all elements of the map gquiz2    cout << "\nThe map gquiz2 after"         << " assign from gquiz1 is : \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }    cout << endl;      // remove all elements up to    // element with key=3 in gquiz2    cout << "\ngquiz2 after removal of"            " elements less than key=3 : \n";    cout << "\tKEY\tELEMENT\n";    gquiz2.erase(gquiz2.begin(), gquiz2.find(3));    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }      // remove all elements with key = 4    int num;    num = gquiz2.erase(4);    cout << "\ngquiz2.erase(4) : ";    cout << num << " removed \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }      cout << endl;      // lower bound and upper bound    // for map gquiz1 key = 5    cout << "gquiz1.lower_bound(5) : "         << "\tKEY = ";    cout << gquiz1.lower_bound(5)->first << '\t';    cout << "\tELEMENT = "         << gquiz1.lower_bound(5)->second << endl;    cout << "gquiz1.upper_bound(5) : "         << "\tKEY = ";    cout << gquiz1.upper_bound(5)->first << '\t';    cout << "\tELEMENT = "         << gquiz1.upper_bound(5)->second << endl;      return 0;}
The map gquiz1 is :KEY ELEMENT1 402 303 604 205 506 507 10
The map gquiz2 after assign from gquiz1 is :KEY ELEMENT1 402 303 604 205 506 507 10
gquiz2 after removal of elements less than key=3 :KEY ELEMENT3 604 205 506 507 10
gquiz2.erase(4) : 1 removedKEY ELEMENT3 605 506 507 10
gquiz1.lower_bound(5) : KEY = 5 ELEMENT = 50gquiz1.upper_bound(5) : KEY = 6 ELEMENT = 50
Multimap in C++ STL 
Multimap is similar to map with an addition that multiple elements can have same keys. Also, it is NOT required that the key value and mapped value pair has to be unique in this case. One important thing to note about multimap is that multimap keeps all the keys in sorted order always. These properties of multimap makes it very much useful in competitive programming.
C++
#include <iostream>#include <iterator>#include <map>  using namespace std;  int main(){    // empty multimap container    multimap<int, int> gquiz1;      // insert elements in random order    gquiz1.insert(pair<int, int>(1, 40));    gquiz1.insert(pair<int, int>(2, 30));    gquiz1.insert(pair<int, int>(3, 60));    gquiz1.insert(pair<int, int>(6, 50));    gquiz1.insert(pair<int, int>(6, 10));      // printing multimap gquiz1    multimap<int, int>::iterator itr;    cout << "\nThe multimap gquiz1 is : \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }    cout << endl;      // adding elements randomly,    // to check the sorted keys property    gquiz1.insert(pair<int, int>(4, 50));    gquiz1.insert(pair<int, int>(5, 10));      // printing multimap gquiz1 again      cout << "\nThe multimap gquiz1 after"         << " adding extra elements is : \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }    cout << endl;      // assigning the elements from gquiz1 to gquiz2    multimap<int, int> gquiz2(gquiz1.begin(),                              gquiz1.end());      // print all elements of the multimap gquiz2    cout << "\nThe multimap gquiz2 after"         << " assign from gquiz1 is : \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }    cout << endl;      // remove all elements up to    // key with value 3 in gquiz2    cout << "\ngquiz2 after removal of"         << " elements less than key=3 : \n";    cout << "\tKEY\tELEMENT\n";    gquiz2.erase(gquiz2.begin(), gquiz2.find(3));    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }      // remove all elements with key = 4    int num;    num = gquiz2.erase(4);    cout << "\ngquiz2.erase(4) : ";    cout << num << " removed \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first             << '\t' << itr->second << '\n';    }      cout << endl;      // lower bound and upper bound    // for multimap gquiz1 key = 5    cout << "gquiz1.lower_bound(5) : "         << "\tKEY = ";    cout << gquiz1.lower_bound(5)->first << '\t';    cout << "\tELEMENT = "         << gquiz1.lower_bound(5)->second         << endl;    cout << "gquiz1.upper_bound(5) : "         << "\tKEY = ";    cout << gquiz1.upper_bound(5)->first << '\t';    cout << "\tELEMENT = "         << gquiz1.upper_bound(5)->second         << endl;      return 0;}
The multimap gquiz1 is :KEY ELEMENT1 402 303 606 506 10
The multimap gquiz1 after adding extra elements is :KEY ELEMENT1 402 303 604 505 106 506 10
The multimap gquiz2 after assign from gquiz1 is :KEY ELEMENT1 402 303 604 505 106 506 10
gquiz2 after removal of elements less than key=3 :KEY ELEMENT3 604 505 106 506 10
gquiz2.erase(4) : 1 removedKEY ELEMENT3 605 106 506 10
gquiz1.lower_bound(5) : KEY = 5 ELEMENT = 10gquiz1.upper_bound(5) : KEY = 6 ELEMENT = 50
Difference between Map and Multimap in C++ STL
cpp-map
cpp-multimap
C++
Difference Between
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.