Multimap of pairs in C++ with Examples - GeeksforGeeks

27 Dec, 2021
What is a multimap?
In C++, a multimap is an associative container that is used to store elements in a mapped fashion. Internally, a multimap is implemented as a red-black tree. Each element of a multimap is treated as a pair. The first value is referred to as key and the second value is referred to as value. Multimap is quite similar to a map but in the case of a multimap, we can have multiple same keys. Also, we cannot use square brackets ([]) to access the value mapped with a key. Like a map, keys of the multimap are sorted in ascending order by default.
Functions associated with multimap:
begin(): Returns an iterator to the first element in the multimap
end(): Returns an iterator to the theoretical element that follows the last element in the multimap
size(): Returns the number of elements in the multimap
max_size(): Returns the maximum number of elements that the multimap can hold
empty(): Returns whether the multimap is empty
insert(key, value): Adds a new element or pair to the multimap
erase(iterator position): Removes the element at the position pointed by the iterator
erase(const x): Removes the key-value ‘x’ from the multimap
clear(): Removes all the elements from the multimap
What is a pair?
Utility header in C++ provides us pair container. A pair consists of two data elements or objects.
The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second).
Pair is used to combine together two values that may be different in type. Pair provides a way to store two heterogeneous objects as a single unit.
Pair can be assigned, copied, and compared. The array of objects allocated in a map or hash_map is of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.
To access the elements, we use variable name followed by dot operator followed by the keyword first or second.
How to access a pair?
The elements of a pair can be accessed by using the dot (.) operator.
Syntax:
auto fistElement = myPair.first;
auto fistElement = myPair.second;
Multimap of pairs
A multimap of pairs is a multimap in which either of the key or value is a pair itself. Two pairs are considered to be equal if the corresponding first and second elements of pairs are equal. Now if there is a need to store more than one copy of a pair along with other elements that too in a sorted or particular order, in such cases multiset of pairs comes in handy.
Syntax:
multimap<pair<dataType1, dataType2>> myMultimap;
Here,
dataType1 and dataType2 can be similar or dissimilar data types.
Example 1: Below is the C++ program to demonstrate the working of a multimap of pairs. 
C++
// C++ program to demonstrate// the working of a multimap of// pairs.#include <bits/stdc++.h>using namespace std;  // Function to print multimap elementsvoid print(multimap<pair<int, int>,            bool>& myContainer){    cout << "Key(pair of integers)" <<             "            " <<             "Value(boolean)\n\n";      for (auto pr : myContainer)      {        pair<int, int> myPair = pr.first;          // pr points to current pair of myContainer        cout << '[' << myPair.first << " , " <<                 myPair.second << ']' <<                 "                                 " <<                 pr.second << '\n';    }}  // Driver codeint main(){    // Declaring a multimap    // Key is of pair<int, int> type    // Value is of bool type    multimap<pair<int, int>, bool> myContainer;      // Creating some pairs to be used    // as keys    pair<int, int> pair1;    pair1 = make_pair(100, 200);      pair<int, int> pair2;    pair2 = make_pair(200, 300);      pair<int, int> pair3;    pair3 = make_pair(300, 400);      pair<int, int> pair4;    pair4 = make_pair(100, 200);      // Since each element is a pair on     // its own in a multimap. So, we     // are inserting a pair    // Note that [] operator doesn't working     // in case of a multimap    myContainer.insert(pair<pair<int, int>,                        bool>(pair1, true));    myContainer.insert(pair<pair<int, int>,                        bool>(pair2, false));    myContainer.insert(pair<pair<int, int>,                        bool>(pair3, true));    myContainer.insert(pair<pair<int, int>,                        bool>(pair4, false));      // Calling print function    print(myContainer);      return 0;}
Key(pair of integers) Value(boolean)
[100, 200] 1[100, 200] 0[200, 300] 0[300, 400] 1
Example 2: Below is the C++ program to demonstrate the working of a multimap of pairs.
C++
// C++ program to demonstrate// the working of a multimap of// pairs.#include <bits/stdc++.h>using namespace std;  // Function to print multimap elementsvoid print(multimap<pair<string, int>,            bool>& myContainer){    cout << "Key(pair of integers)" <<             "            " <<             "Value(boolean)\n\n";      for (auto pr : myContainer)     {        pair<string, int> myPair = pr.first;          // pr points to current pair of myContainer        cout << '[' << myPair.first <<                 " , " << myPair.second <<                 ']' << "                " <<                 "                 " <<                 pr.second << '\n';    }}  // Driver codeint main(){    // Declaring a multimap    // Key is of pair<int, int> type    // Value is of bool type    multimap<pair<string, int>, bool> myContainer;      // Creating some pairs to be used    // as keys    pair<string, int> pair1;    pair1 = make_pair("GFG", 100);      pair<string, int> pair2;    pair2 = make_pair("C++", 200);      pair<string, int> pair3;    pair3 = make_pair("CSS", 300);      pair<string, int> pair4;    pair4 = make_pair("GFG", 400);      // Since each element is a pair on its     // own in a multimap. So, we are     // inserting a pair    // Note that [] operator doesn't working     // in case of a multimap    myContainer.insert(pair<pair<string, int>,                        bool>(pair1, true));    myContainer.insert(pair<pair<string, int>,                        bool>(pair2, false));    myContainer.insert(pair<pair<string, int>,                        bool>(pair3, true));    myContainer.insert(pair<pair<string, int>,                        bool>(pair4, false));      // Calling print function    print(myContainer);      return 0;}
Key(pair of integers) Value(boolean)
[C++, 200] 0[CSS, 300] 1[GFG, 100] 1[GFG, 400] 0
cpp-multimap
cpp-pair
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
Queue in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++