Difference between pair in Multiset and Multimap in C++ STL Last Updated : 10 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Pairs in C++: The pair container is a simple container defined in <utility> header consisting 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 which may be different in type. Pair provides a way to store two heterogeneous objects as a single unit. Syntax: pair (data_type1, data_type2) Pair_name; Multiset in C++: Multiset is a type of associative containers that store elements following a specific order, and where multiple elements can have same values. Syntax: multiset <data_type> Multiset_name; Multimap: Multi-map is a type of associative containers which is similar to the map with an exception that multiple elements can have same keys. Syntax: multimap <data_type1, data_type2> Multimap_name What is the difference between pair in multi-set and multi-map in C++ STL? The default behavior of both of these data structures multiset and multimap is to store elements in ascending order. When a pair of multiset is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pairs are equal then it will sort the pair according to the second element of the pair. When a pair of multimap is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pair are equal then it will print the pair according to the order of insertion to the pair of multimap. Below are the programs to illustrate the difference: Program 1: Pair in multi-set CPP // C++ program print the data of // multiset by inserting using pair #include <bits/stdc++.h> using namespace std; // Function to print the data stored // in pair of multiset void printData(multiset<pair<int, string> > gfg) { // Declare iterator multiset<pair<int, string> >::iterator i; // Iterate through pair of multiset for (i = gfg.begin(); i != gfg.end(); ++i) { // Print the pairs cout << i->first << " " << i->second << endl; } } // Driver Code int main() { // Declare pair of multiset multiset<pair<int, string> > gfg; // Insert Data gfg.insert(make_pair(1, "yukti")); gfg.insert(make_pair(2, "umang")); gfg.insert(make_pair(3, "vinay")); gfg.insert(make_pair(3, "vijay")); gfg.insert(make_pair(4, "kanak")); // Function call to print the data printData(gfg); return 0; } Explanation:In the above program we have created pairs of integer and string in which names are paired with each integer and are inserted in the multi-set. According to default behavior of multi-set the data is arranged in ascending order according to first element but when first element is same it will arrange those elements according to the second value. For the pair (3, "vijay") and (3, "vinay") the first element in the pair i.e., 3 is same for both “vijay” and “vinay” so it will arrange the pairs according to second element “vijay” then “vinay” (in alphabetical sequence). Program 2: Pair in multi-map CPP // C++ program print the data of // multimap by inserting using pair #include <bits/stdc++.h> using namespace std; // Function to print the data stored // in pair of multimap void printData(multimap<int, string> gfg) { // Declare iterator multimap<int, string>::iterator i; // Iterate through pair of multiset for (i = gfg.begin(); i != gfg.end(); ++i) { // Print the pairs cout << i->first << " " << i->second << endl; } } // Driver Code int main() { // Declare pair of multimap multimap<int, string> gfg; // Insert data gfg.insert(make_pair(1, "yukti")); gfg.insert(make_pair(2, "umang")); gfg.insert(make_pair(3, "vinay")); gfg.insert(make_pair(3, "vijay")); gfg.insert(make_pair(4, "kanak")); // Function call to print the data printData(gfg); return 0; } Output1 yukti 2 umang 3 vinay 3 vijay 4 kanak Explanation of above Code:In the above program, we have again inserted the same pairs but this time in multi-map . According to default behavior of multi-map the data is arranged in ascending order according to key but when key is same unlike the multi-set it will see the precedence which element is inserted first and then it will arrange according to that sequence. So, as in the output shown we can see that as the key 3 is same for “vinay” and “vijay” so it will follow the sequence in which the pairs were inserted in the multi-map, that’s why, "vinay" came first before "vijay" in output. Tabular Differentiation: Pair in MultisetMultimapIn pair of multiset pair is used to mapped key with specific value.Default behaviour is to insert element as a key-value pair.When a pair of a multiset is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pairs are equal then it will sort the pair according to the second element of the pair.When a pair of a multimap is created then by default, it will sort all the pairs in increasing order according to the first element of all the pairs and if the first element of any two or more than two pair are equal then it will print the pair according to the order of insertion to the pair of multimap. Syntax: multiset<pair<int, string> > M; Syntax: multimap<int, string> M; Time complexity of inserting a pair in Multiset is O(log N)Time complexity of inserting a pair in Multimap is O(log N)Time complexity of deleting pairs in Multiset is O(N)Time complexity of deleting pairs in Multimap is vary and not specified. Comment More infoAdvertise with us Next Article Multimap vs Map in C++ STL with Examples yagyeshbagaya Follow Improve Article Tags : Competitive Programming C++ Programs Difference Between C++ DSA STL cpp-pair cpp-multimap cpp-multiset +5 More Practice Tags : CPPSTL Similar Reads Multimap in C++ STL In C++, multimap is an associative container similar to map, but it can have multiple elements with same keys. It stores all the elements in increasing order based on their keys by default but can be changed if required. It provides fast insertion, deletion and search on this sorted data.Example:CPP 8 min read Commonly Used Methodsmultimap::begin() and multimap::end() in C++ STLmultimap::begin() is a built-in function in C++ STL that returns an iterator referring to the first element in the multimap container. Since the multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container's sorting cri 3 min read multimap size() function in C++ STLThe multimap::size() is a built-in function in C++ STL which returns the number of elements in the multimap container. Syntax: multimap_name.size() Parameters: The function does not accept any parameter. Return Value: This function returns the number of elements a multimap container has. CPP // C++ 1 min read multimap empty() function in C++ STLThe multimap::empty() is a boolean type observer function in C++ STL which tells whether the container is empty or not. This function returns true when the multimap container is empty (i.e. the size of the container is 0). Being an observer function it does not modify the multimap in any way. Syntax 1 min read multimap insert() in C++ STLThe multimap::insert is a built-in function in C++ STL that is used to insert elements in the multimap container. Syntax: iterator multimap_name.insert({key, element}) Parameters: The function accepts a pair that consists of a key and element which is to be inserted into the multimap container. Retu 2 min read multimap::emplace() in C++ STLThe multimap::emplace() is a built-in function in C++ STL which inserts the key and its element in the multimap container. It effectively increases the container size by one as multimap is the container that stores multiple keys with same values. Syntax: multimap_name.emplace(key, element) Parameter 1 min read multimap find() in C++ STLmultimap::find() is a built-in function in C++ STL which returns an iterator or a constant iterator that refers to the position where the key is present in the multimap. In case of multiple same keys being present, the iterator that refers to one of the keys (typically the first one). In case we wis 2 min read multimap::count() in C++ STLThe multimap::count is a built-in function in C++ STL which returns the number of times a key is present in the multimap container. Syntax: multimap_name.count(key) Parameters: The function accepts one mandatory parameter key which specifies the key whose count in multimap container is to be returne 1 min read multimap::erase() in C++ STLmultimap::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range. Syntax for erasing a key: multimap_name.erase(key) Parameters: The function accepts one mandatory parameter key wh 4 min read multimap clear() function in C++ STLThe multimap clear() function is an inbuilt function in C++ STL which is used to remove all elements from the multimap container (which are destroyed), leaving the container with a size of 0. Syntax : mymultimap_name.clear() Parameters: This function does not take any arguments. Return Value: This f 2 min read multimap::swap() in C++ STLmultimap::swap() is used to swap the contents of one multimap with another multimap of same type and size. Syntax:- multimap1.swap(multimap2) Parameters : The name of the multimap with which the contents have to be swapped. Result : All the elements of the 2 multimap are swapped. Examples: Input: mu 2 min read Other Member Methodsmultimap maxsize() in C++ STLThe multimap::max_size() is a built-in function in C++ STL which returns the maximum number of elements a multimap container can hold. Syntax: multimap_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a list 1 min read multimap equal_range() in C++ STLThe multimap::equal_range() is a built-in function in C++ STL which returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k. If there are no matches with key K, the range returned is of length 0 with both 2 min read multimap upper_bound() function in C++ STLThe multimap::upper_bound(k) is a built-in function in C++ STL which returns an iterator pointing to the immediate next element which is just greater than k. If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points to key+1 and element=0. Syntax: 2 min read multimap lower_bound() function in C++ STLThe multimap::lower_bound(k) is a built-in function in C++ STL which returns an iterator pointing to the key in the container which is equivalent to k passed in the parameter. In case k is not present in the multimap container, the function returns an iterator pointing to the immediate next element 2 min read multimap rbegin in C++ STLmultimap::rbegin() is a built-in-function in C++ STL which returns an iterator pointing to the last element of the container. Syntax: multimap_name.rbegiin() Parameters: The function does not take any parameter. Return Value: The function returns a reverse iterator pointing to the last element of th 2 min read multimap::cbegin() and multimap::cend() in C++ STLmultimap::cbegin() is a built-in function in C++ STL which returns a constant iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sort 3 min read multimap rend in C++ STLmultimap::rend() is a built-in function in C++ STL which returns a reverse iterator pointing to the theoretical element preceding to the first element of the multimap container. Syntax multimap_name.rend() Parameters: The function does not take any parameter. Return ValueThe function returns a rever 2 min read multimap value_comp() function in C++ STLThe multimap::value_comp() method returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second. Here the 1st object compares the object of type std::multimap::type. The arguments taken by this function object are of member type t 2 min read multimap::emplace_hint() in C++ STLThe multimap::emplace_hint() is a built-in function in C++ STL which inserts the key and its element in the multimap container with a given hint. It effectively increases the container size by one as multimap is the container that stores multiple keys with same values. The hint provided does not aff 2 min read multimap::crbegin() and multimap::crend() in C++ STLmultimap::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator referring to the last element in the multimap container. Since multimap container contains the element in an ordered way, crbegin() will point to that element that will come last according to the containe 3 min read multimap::operator= in C++ STLmultimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. Syntax:- multimap1 = (multimap2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as p 2 min read multimap key_comp() in C++ STLThe std::multimap::key_comp() is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container.By default, this is a less object, which returns the same as operator â<'.It is a function pointer or a function object which takes two arguments of the same ty 2 min read Multimap of pairs in C++ with Examples 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 5 min read Multimap of tuples in C++ with Examples 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 6 min read Difference between pair in Multiset and Multimap in C++ STL Pairs in C++: The pair container is a simple container defined in <utility> header consisting 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 which 5 min read Multimap vs Map in C++ STL with Examples 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 u 8 min read Common Multimap ProblemsDescending Order in Map and Multimap of C++ STLWe have discussed map in C++ STL and multimap in C++ STL. The default behavior of these data structures is to store elements in ascending order. How to store elements in reverse order or descending order when inserting in map and multimap? We can use the third parameter, that is std::greater along w 3 min read How to traverse through all values for a given key in multimap?Given a multimap and a key of the multimap, our task is to simply display the (key - value) pairs of the given key. In multimap we can have multiple (key - value) pair for the same key. Suppose our multimap contains key value1 102 202 302 403 504 604 70key : 2key value2 202 302 40Like in unordered_m 5 min read Like