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; } |
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
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:
- set::rbegin() and set::rend() in C++ STL
- Count number of unique Triangles using STL | Set 1 (Using set)
- set::begin() and set::end() in C++ STL
- bitset set() function in C++ STL
- set value_comp() function in C++ STL
- set upper_bound() function in C++ STL
- set insert() function in C++ STL
- set find() function in C++ STL
- set max_size() function in C++ STL
- set emplace_hint() function in C++ STL
- set count() function in C++ STL
- set lower_bound() function in C++ STL
- set cbegin() and cend() function in C++ STL
- set crbegin() and crend() function in C++ STL
- Dijkstra’s shortest path algorithm using set in STL
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Counting Inversions using Set in C++ STL
- Insertion and Deletion in STL Set C++
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.

