std::equal_range (3) - Linux Manuals
std::equal_range: std::equal_range
NAME
std::equal_range - std::equal_range
Synopsis
Defined in header <algorithm>
template< class ForwardIt, class T >
std::pair<ForwardIt,ForwardIt> (until C++20)
equal_range( ForwardIt first, ForwardIt last,
const T& value );
template< class ForwardIt, class T >
constexpr std::pair<ForwardIt,ForwardIt> (since C++20)
equal_range( ForwardIt first, ForwardIt last,
const T& value ); (1)
template< class ForwardIt, class T, class Compare >
std::pair<ForwardIt,ForwardIt> (until C++20)
equal_range( ForwardIt first, ForwardIt last,
const T& value, Compare comp ); (2)
template< class ForwardIt, class T, class Compare >
constexpr std::pair<ForwardIt,ForwardIt> (since C++20)
equal_range( ForwardIt first, ForwardIt last,
const T& value, Compare comp );
Returns a range containing all elements equivalent to value in the range [first, last).
The range [first, last) must be at least partially ordered with respect to value, i.e. it must satisfy all of the following requirements:
* partitioned with respect to element < value or comp(element, value) (that is, all elements for which the expression is true precedes all elements for which the expression is false)
* partitioned with respect to !(value < element) or !comp(value, element)
* for all elements, if element < value or comp(element, value) is true then !(value < element) or !comp(value, element) is also true
A fully-sorted range meets these criteria.
The returned range is defined by two iterators, one pointing to the first element that is not less than value and another pointing to the first element greater than value. The first iterator may be alternatively obtained with std::lower_bound(), the second - with std::upper_bound().
The first version uses operator< to compare the elements, the second version uses the given comparison function comp.
Parameters
first, last - the range of elements to examine
value - value to compare the elements to
comp - While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value_category (thus, Type1 & is not allowed
Type requirements
-
ForwardIt must meet the requirements of LegacyForwardIterator.
-
Compare must meet the requirements of BinaryPredicate. it is not required to satisfy Compare
Return value
std::pair containing a pair of iterators defining the wanted range, the first pointing to the first element that is not less than value and the second pointing to the first element greater than value.
If there are no elements not less than value, last is returned as the first element. Similarly if there are no elements greater than value, last is returned as the second element
Complexity
The number of comparisons performed is logarithmic in the distance between first and last (At most 2 * log
2(last - first) + O(1) comparisons). However, for non-LegacyRandomAccessIterators, the number of iterator increments is linear.
Possible implementation
First version
Second version
Example
// Run this code