multimap::crbegin() and multimap::crend() in C++ STL Last Updated : 29 Dec, 2022 Comments Improve Suggest changes Like Article Like Report multimap::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 container's sorting criterion. Syntax: multimap_name.crbegin() Parameters: The function does not accept any parameter. Return Value: The function returns a constant reverse iterator referring to the last element in the multimap container. Simple Example : C++ #include <vector> #include <iostream> int main() { std::vector<int> vec {1, 2, 3, 4, 5}; // Iterate through the elements of vec in reverse order for (auto it = vec.crbegin(); it != vec.crend(); ++it) { std::cout << *it << ' '; } std::cout << std::endl; return 0; } Output5 4 3 2 1 C++ // C++ program to illustrate // multimap::crbegin() function #include <bits/stdc++.h> using namespace std; int main() { // initialize container multimap<int, int> mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 4, 20 }); mp.insert({ 5, 50 }); auto ite = mp.crbegin(); cout << "The last element is {" << ite->first << ", " << ite->second << "}\n"; // prints the elements cout << "\nThe multimap in reverse order is: \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } OutputThe last element is {5, 50} The multimap in reverse order is: KEY ELEMENT 5 50 4 20 3 60 2 30 1 40multimap::crend() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the theoretical element before the first element in the multimap. Since multimap container contains the element in an ordered way, crend() will point to the element theoretically before the first element according to the container's sorting criterion. Syntax: multimap_name.crend() Parameters: The function does not accept any parameter. Return Value: The function returns a constant reverse iterator pointing to the theoretical element before the first element in the multimap. C++ // C++ program to illustrate // multimap::crend() function #include <bits/stdc++.h> using namespace std; int main() { // initialize container multimap<int, int> mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 4, 20 }); mp.insert({ 5, 50 }); // prints the elements cout << "\nThe multimap in reverse order is : \n"; cout << "KEY\tELEMENT\n"; for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0; } OutputThe multimap in reverse order is : KEY ELEMENT 5 50 4 20 3 60 2 30 1 40 Let us see the differences in a tabular form -: multimap::crbegin() multimap::crend() 1.It is used to return a const_reverse_iterator pointing to the last element in the containerIt is used to return a const_reverse_iterator pointing to the theoretical element preceding the first element in the container 2. Its syntax is -: const_reverse_iterator crbegin(); Its syntax is -: const_reverse_iterator crend(); 3.It does not take any parameters.It does not take any parameters.4.Its complexity is constant.Its complexity is constant.5.Its iterator validity does not change.Its iterator validity does not change. Comment More infoAdvertise with us Next Article multimap::operator= in C++ STL gopaldave Follow Improve Article Tags : Misc C++ STL CPP-Functions cpp-multimap +1 More Practice Tags : CPPMiscSTL 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