Skip to content
Related Articles

Related Articles

Improve Article
set equal_range() function in C++ STL
  • Difficulty Level : Easy
  • Last Updated : 10 Jul, 2018

The set::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. Since set contains unique elements, the lower bound will be the element itself and the upper bound will point to the next element after key k. If there are no elements matching key K, the range returned is of length 0 with both iterators pointing to the first element which is greater than k according to the container’s internal comparison object (key_comp). If the key exceeds the maximum element in the set container, it returns an iterator pointing to the last element in the set container.

Syntax:

set_name.equal_range(key) 

Parameters: The function accepts one mandatory parameter key which specifies the key whose range in the set container is to be returned.

Return Value: The function returns an iterator of pairs. (key_comp). The pair refers to the range that includes all the elements in the container which have a key equivalent to k. Since set contains unique elements, the lower bound will be the element itself and the upper bound will point to the next element after key k. If there are no elements matching key K, the range returned is of length 0 with both iterators pointing to the first element which is greater than k according to the container’s internal comparison object (key_comp). If the key exceeds the maximum element in the set container, it returns an iterator pointing to the last element in the set container.

Below program illustrates the above function.




// CPP program to demonstrate the
// set::equal_range() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
  
    set<int> s;
  
    s.insert(1);
    s.insert(4);
    s.insert(2);
    s.insert(5);
    s.insert(3);
  
    // prints the set elements
    cout << "The set elements are: ";
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
  
    // Function returns lower bound and upper bound
    auto it = s.equal_range(2);
    cout << "\nThe lower bound of 2 is " << *it.first;
    cout << "\nThe upper bound of 2 is " << *it.second;
  
    // Function returns the last element
    it = s.equal_range(8);
    cout << "\nThe lower bound of 8 is " << *it.first;
    cout << "\nThe upper bound of 8 is " << *it.second;
  
    // Function returns the range where the
    // element greater than 0 lies
    it = s.equal_range(0);
    cout << "\nThe lower bound of 0 is " << *it.first;
    cout << "\nThe upper bound of 0 is " << *it.second;
  
    return 0;
}
Output:
The set elements are: 1 2 3 4 5 
The lower bound of 2 is 2
The upper bound of 2 is 3
The lower bound of 8 is 5
The upper bound of 8 is 5
The lower bound of 0 is 1
The upper bound of 0 is 1
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :