The 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 iterators pointing to the first element that has a key considered to go after k according to the container’s internal comparison object (key_comp).
Syntax:
iterator multimap_name.equal_range(key)
Parameters: This function accepts a single mandatory parameter key which specifies the element whose range in the container is to be returned.
Return Value: The function 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 iterators pointing to the first element that has a key considered to go after k according to the container’s internal comparison object (key_comp).
Program below illustrate the above method:
C++
// C++ program to illustrate the// multimap::equal_range() 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({ 1, 20 }); mp.insert({ 5, 50 }); // Stores the range of key 1 auto it = mp.equal_range(1); cout << "The multimap elements of key 1 is : \n"; cout << "KEY\tELEMENT\n"; // Prints all the elements of key 1 for (auto itr = it.first; itr != it.second; ++itr) { cout << itr->first << '\t' << itr->second << '\n'; } return 0;} |
The multimap elements of key 1 is : KEY ELEMENT 1 40 1 20
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- multimap::cbegin() and multimap::cend() in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- multimap::crbegin() and multimap::crend() in C++ STL
- Descending order in Map and Multimap of C++ STL
- multimap::operator= in C++ STL
- multimap::swap() in C++ STL
- multimap::emplace_hint() in C++ STL
- multimap find() in C++ STL
- multimap insert() in C++ STL
- multimap size() function in C++ STL
- multimap lower_bound() function in C++ STL
- multimap upper_bound() function in C++ STL
- multimap clear() function in C++ STL
- multimap rend in C++ STL
- multimap rbegin in C++ STL
- multimap empty() function in C++ STL
- multimap value_comp() function in C++ STL
- multimap key_comp() in C++ STL
- multimap get_allocator() function in C++ STL
- multimap key_comp in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : arorakashish0911

