map equal_range() in C++ STL

The map::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. Since the map container only contains unique key, hence the first iterator in the pair returned thus points to the element and the second iterator in the pair points to the next key which comes after key K. If there are no matches with key K, the range returned is of length 1 with both iterators pointing to the an element which has a key denoting the size of map and elements as 0.

Syntax:

iterator map_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 as explained above.

Below programs illustrate the above method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// map::equal_range() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize container
    map<int, int> mp;
  
    // insert elements in random order
    mp.insert({ 4, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 6, 60 });
  
    pair<map<int, int>::iterator, map<int, int>::iterator> it;
  
    // iterator of pairs
    it = mp.equal_range(1);
    cout << "The lower bound is " << 
    it.first->first << ":" << it.first->second;
  
    cout << "\nThe upper bound is " << 
    it.second->first << ":" << it.second->second;
  
    return 0;
}

chevron_right


Output:

The lower bound is 1:40
The upper bound is 4:30

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// map::equal_range() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize container
    map<int, int> mp;
  
    // insert elements in random order
    mp.insert({ 4, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 6, 60 });
  
    pair<map<int, int>::iterator, map<int, int>::iterator> it;
  
    // iterator of pairs
    it = mp.equal_range(10);
    cout << "The lower bound is " << 
    it.first->first << ":" << it.first->second;
  
    cout << "\nThe upper bound is " << 
    it.second->first << ":" << it.second->second;
  
    return 0;
}

chevron_right


Output:

The lower bound is 3:0
The upper bound is 3:0


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.