set equal_range() function in C++ STL
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
Recommended Posts:
- Function Overloading vs Function Overriding in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- log() function in C++
- exp() function C++
- div() function in C++
- fma() function in C++
- mbrlen() function in C/C++
- wcspbrk() function in C/C++
- wcsrchr() function in C/C++
- c32rtomb() function in C/C++
- c16rtomb() function in C/C++
- mbrtowc() function in C/C++
- iswxdigit() function in C/C++
- towlower() function in C/C++
- iswspace() function in C/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.



