Multiset of Pairs in C++ with Examples Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report What is Multiset? A multiset is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can contain multiple occurrences of the same element. Some of the functions associated with a multiset: begin(): Returns an iterator to the first element in the multiset. end(): Returns an iterator to the theoretical element that follows the last element in the multiset.size(): Returns the number of elements in the multiset.max_size(): Returns the maximum number of elements that the multiset can hold.empty(): Returns whether the multiset is empty.What is 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? To access elements of a pair use the dot (.) operator. Syntax: auto fistElement = myPair.first; auto fistElement = myPair.second;Multiset of pairs A multiset of pairs is a multiset in which each element 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 particular order, in such cases multiset of pairs comes in handy. Syntax: multiset<pair<dataType1, dataType2>> myMultiset; Here, dataType1 and dataType2 can be similar or dissimilar data types. Example 1: Below is the C++ program to demonstrate the working of a multiset of pairs having integer values. C++ // C++ program to illustrate the // implementation of multiset of // pairs #include <bits/stdc++.h> using namespace std; // Function to print multiset // elements void print(multiset<pair<int, int>> &multisetOfPairs) { // Iterating over multiset of // pairs elements for (auto cuurentPair : multisetOfPairs) { // Each element is a tuple itself pair<int, int> pr = cuurentPair; // Printing pair elements cout << "[ " << pr.first << ' ' << pr.second << " ]"<< '\n'; } } // Driver code int main() { // Declaring a multiset of tuples multiset<pair<int, int>> multisetOfPairs; // Initializing a pair pair<int, int> pair1; pair1 = make_pair(1, 2); // Initializing a pair pair<int, int> pair2; pair2 = make_pair(3, 4); // Initializing another pair pair<int, int> pair3; pair3 = make_pair(5, 6); // Initializing another pair pair<int, int> pair4; pair4 = make_pair(7, 8); // Initializing another pair pair<int, int> pair5; pair5 = make_pair(9, 10); // Inserting into multiset multisetOfPairs.insert(pair1); multisetOfPairs.insert(pair2); multisetOfPairs.insert(pair3); multisetOfPairs.insert(pair4); multisetOfPairs.insert(pair5); // Calling print function print(multisetOfPairs); return 0; } Output: [ 1 2 ][ 3 4 ][ 5 6 ][ 7 8 ][ 9 10 ] Time complexity: O(n* log n). //n is the size of the multiset. Space complexity: O(n). Explanation: In the above output, the elements are arranged in sorted order of pairs in the multiset of pairs. Example 2: Below is the C++ program to demonstrate the working of a multiset of pairs having string values. C++ // C++ program to illustrate the // implementation of multiset of // pairs #include <bits/stdc++.h> using namespace std; // Function to print multiset elements void print(multiset<pair<string, string>> &multisetOfPairs) { // Iterating over multiset of pairs elements for (auto currentPair : multisetOfPairs) { // Each element is a pair itself pair<string, string> pr = currentPair; // Printing pair elements cout << "[ " << pr.first << ' ' << pr.second << " ]"<< '\n'; } } // Driver code int main() { // Declaring a multiset of pairs multiset<pair<string, string>> multisetOfPairs; // Initializing a pair pair<string, string> pair1; pair1 = make_pair("GeeksforGeeks", "GFG"); // Initializing a pair pair<string, string> pair2; pair2 = make_pair("Swift", "Python"); // Initializing another pair pair<string, string> pair3; pair3 = make_pair("C++", "C"); // Initializing another pair pair<string, string> pair4; pair4 = make_pair("PHP", "HTML"); // Initializing another pair pair<string, string> pair5; pair5 = make_pair("Javascript", "CSS"); // Inserting into multiset multisetOfPairs.insert(pair1); multisetOfPairs.insert(pair2); multisetOfPairs.insert(pair3); multisetOfPairs.insert(pair4); multisetOfPairs.insert(pair5); // Calling print function print(multisetOfPairs); return 0; } Output: [ C++ C ][ GeeksforGeeks GFG ][ Javascript CSS ][ PHP HTML ][ Swift Python ] Time complexity: O(n* log n). //n is the size of the multiset. Space complexity: O(n). Explanation: In the above output, the elements are arranged in sorted order of pairs in the multiset of pairs. Comment More infoAdvertise with us Next Article Multiset of Vectors in C++ with Examples bhuwanesh Follow Improve Article Tags : C++ STL cpp-pair cpp-multiset Practice Tags : CPPSTL Similar Reads Multiset in C++ STL In C++, multiset is an associative container similar to the set, but it can store multiple elements with same value. It is sorted in increasing order by default, but it can be changed to any desired order. It provides fast insertion, deletion and search operations.Example:C++#include <iostream 6 min read Commonly Used Methodsmultiset begin() and end() function in C++ STLThe multiset::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the multiset container. Since multiset always contains elements in an ordered way, begin() always points to the first element according to the sorting criterion. Syntax: iterator multise 2 min read multiset size() in C++ STL with ExamplesThe multiset::size() is a built-in function in C++ STL which returns the number of elements in the multiset container. Syntax: multiset_name.size() Parameters: The function does not accept any parameters. Return Value: The function returns the number of elements in the multiset container. Below prog 2 min read multiset empty() function in C++ STLThe multiset::empty() function is a built-in function in C++ STL which checks if the multiset is empty or not. It returns true if the multiset is empty, else it returns false. Syntax: multiset_name.empty() Parameters: The function does not accept any parameter. Return Value: The function returns tru 1 min read multiset insert() function in C++ STLThe multiset::insert() is a built-in function in C++ STL which insert elements in the multiset container or inserts the elements from a position to another position from one multiset to a different multiset. Syntax: iterator multiset_name.insert(element)Time Complexity: O(log n) Since the elements a 4 min read multiset::emplace() in C++ STLMultisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::emplace() This function is used to insert a new element into the multiset container. Syntax : multisetname.emplace(value) Parameters : The element to be inserted in 3 min read multiset find() function in C++ STLThe multiset::find() is a built-in function in C++ STL which returns an iterator pointing to the lower_bound of the element which is searched in the multiset container. If the element is not found, then the iterator points to the position past the last element in the set. Syntax: multiset_name.find( 2 min read multiset count() function in C++ STLThe multiset::count() function is a built-in function in C++ STL that searches for a specific element in the multiset container and returns the number of occurrences of that element. Syntax: multiset_name.count(val) Parameters: The function accepts a single parameter val which specifies the element 2 min read multiset::erase() in C++ STLThe std::multiset::erase() is a built-in STL function used to remove elements from the multiset container. It is member function of std::multiset class defined inside <multiset> header file. In this article, we will learn about std::multiset::erase() in C++.The multiset::erase() function provi 3 min read multiset clear() function in C++ STLThe multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multiset container after removal is 0. Syntax: multiset_name.clear() Parameters: The function does not accept any parameter. Return Value: The function does not 2 min read multiset::swap() in C++ STLMultisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::swap() This function is used to exchange the contents of two multisets but the sets must be of same type, although sizes may differ. Syntax : multisetname1.swap(mul 3 min read Other Member Functionsmultiset lower_bound() in C++ STL with ExamplesThe multiset::lower_bound() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter. In case k is not present in the set container, the function returns an iterator pointing to the immediate next elem 3 min read multiset upper_bound() in C++ STL with ExamplesThe multiset::upper_bound() is a built-in function in C++ STL that 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 an element which points to the po 3 min read multiset max_size() in C++ STLThe multiset::max_size() is an observer function in C++ STL which returns the maximum number of elements a container can hold. This limit might be due to system or library implementations. Being an observer function it does not modify the multiset in any way. Syntax: multiset_name.max_size() Paramet 1 min read multiset::operator= in C++ STLMultisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::operator= This 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. 3 min read multiset equal_range() function in C++ STLThe multiset::equal_range() is a built-in function in C++ STL which returns an iterator of pairs. The pair refers to the range that includes all the elements in the container which have a key equivalent to k. The lower bound will be the element itself and the upper bound will point to the next eleme 3 min read multiset emplace_hint() function in C++ STLThe multiset::emplace_hint() is a built-in function in C++ STL which inserts a new element in the multiset. A position is passed in the parameter of the function which acts as a hint from where the searching operation starts before inserting the element at its current position. The position only hel 3 min read multiset rbegin() and rend() function in C++ STLmultiset::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the multiset container. Syntax: reverse_iterator multiset_name.rbegin() Parameters: The function does not take any parameter. Return value: The function returns a reverse iterator po 2 min read multiset cbegin() and cend() function in C++ STLThe multiset::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: con 3 min read multiset crbegin() and crend() function in C++ STLThe multiset::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the multiset container. The iterators can be increased or decreased to traverse the set accordingly 2 min read multiset get_allocator() function in C++ STLThe multiset::get_allocator() method in C++ STL is a built-in function in C++ STL which returns a copy of the allocator object associated with the multiset. Syntax: multiset_name.get_allocator() where allocator_type is the type of the allocator used by the container. Parameters: The function does no 1 min read Multiset of Pairs in C++ with Examples What is Multiset? A multiset is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can contain multiple occurrences of the same element. Some of the functions associated with a multiset: begin(): Returns an iterator to the first element in the m 4 min read Multiset of Vectors in C++ with Examples What is Multiset? A multiset in C++ is an associative container that can hold a number of elements in a specific order. Unlike a set, a multiset can hold multiple copies of the same element. Functions associated with a multiset: begin(): Returns an iterator to the first element in the multiset.end() 4 min read Multiset of Tuples in C++ with Examples What is a tuple? A tuple in C++ is an object which binds a group of elements together. The elements can be similar as well as different data types. The elements of tuples are initialized as in the order in which they will be accessed. Syntax: tuple<data_type1, data_type2, dataType3, ....> myTu 5 min read Multi-set for user defined data type You are given Q queries. Each query contains an integer k and a person's information i.e, first name, last name, age. For each query, we need to output Kth person among them if all person information are arrange in ascending order. Note: Person A comes before person B if first name of A is lexicogra 3 min read Like