In C++, std::equal_range() returns a pair of iterators that mark the start and end of all elements equal to a value in a sorted range.
It does the work of lower_bound() and upper_bound() in one call. If the value is not found, both iterators are the same, showing where it would be inserted.
Syntax
Add #include <algorithm>, then call std::equal_range(). Start with these two lines.
C++
auto r = std::equal_range(v.begin(), v.end(), 2);
r.first, r.second
Call
What it does
auto r = std::equal_range(v.begin(), v.end(), 2);
Get both ends of the group of 2s.
r.first, r.second
The start of the group and the position after its last element.
Library declarations
C++
template<class ForwardIt, class T>
std::pair<ForwardIt, ForwardIt>
equal_range(ForwardIt first, ForwardIt last, const T& value);
Parameters
Parameter
Description
first, last
Required. A sorted range.
value
Required. The value to look for.
comp
Optional. The comparison that was used to sort the range.
Return Value
Return Value
Description
std::pair
first is the lower bound and second is the upper bound.