Defined in header
template<classForwardIt,classT>std::pair<ForwardIt,ForwardIt>equal_range(ForwardItfirst,ForwardItlast,constT&value); (1)(constexpr since C++20)
(until C++26)template<classForwardIt,classT=typenamestd::iterator_traits<ForwardIt>::value_type>constexprstd::pair<ForwardIt,ForwardIt>equal_range(ForwardItfirst,ForwardItlast,constT&value);(since C++26)template<classForwardIt,classT,classCompare>std::pair<ForwardIt,ForwardIt>equal_range(ForwardItfirst,ForwardItlast,constT&value,Comparecomp); (2)(constexpr since C++20)
(until C++26)template<classForwardIt,classT=typenamestd::iterator_traits<ForwardIt>::value_type,classCompare>constexprstd::pair<ForwardIt,ForwardIt>equal_range(ForwardItfirst,ForwardItlast,constT&value,Comparecomp);(since C++26)Searches for the range containing all elements equivalent to value in the partitioned source range [first, last). An element is considered equivalent to value if it neither orders before nor orders after value.
1) The order is determined by operator<(until C++20)std::less{}(since C++20).
Given two expressions bool(e<value) and !bool(value<e)(until C++20)bool(std::less{}(e,value)) and !bool(std::less{}(value,e))(since C++20). If any of the following conditions is satisfied, the behavior is undefined:
The elements e of the source range are not
with respect to these two expressions at the same time.
There exists an element e in the source range such that these two expressions yield different values.
2) The order is determined by comp.
Given two expressions bool(comp(e,value)) and !bool(comp(value,e)). If any of the following conditions is satisfied, the behavior is undefined:
The elements e of the source range are not partitioned with respect to these two expressions at the same time.
There exists an element e in the source range such that these two expressions yield different values.
Parameters
first, last - the pair of iterators defining the source
value - value to compare the elements to comp - binary predicate which returns true if the first argument is ordered before the second. The signature of the predicate function should be equivalent to the following:
boolpred(constType1&a,constType2&b);
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
(thus, Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
The type Type1 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type1. The type Type2 must be such that an object of type T can be implicitly converted to Type2.
Type requirements -ForwardIt must meet the requirements of
. -Compare must meet the requirements of
. It is not required to satisfy
. Return value
A
object, where:
The data member first holds an iterator to the first element which is not ordered beforevalue, or last if no such element is found.
The data member second holds an iterator to the first element which is ordered after value, or last if no such element is found.
Complexity
Given N as std::distance(first,last):
1) At most 2log2(N)+𝓞(1) comparisons with value using operator<(until C++20)std::less{}(since C++20).
2) At most 2log2(N)+𝓞(1) applications of the comparator comp.
Notes
Although std::equal_range only requires the source range to be partitioned, this algorithm is usually used in the case where the source range is sorted, so that the binary search is valid for any value.
On top of the requirements of
and
, std::equal_range also requires operator< or comp to be asymmetric (i.e., a<b and b<a always have different results).
Therefore, the intermediate results of binary search can be shared by
and
. For example, the result of the
call can be used as the argument of first in the
call.
If ForwardIt is not a
, the number of iterator increments is linear in std::distance(first,last). Notably,
and
iterators are not random access, and so their member functions
(resp.
) should be preferred.
macroValueStdFeature
__cpp_lib_algorithm_default_value_type
(C++26)
for algorithms (
)Possible implementation
template<classForwardIt,classT=typenamestd::iterator_traits<ForwardIt>::value_type>constexprstd::pair<ForwardIt,ForwardIt>equal_range(ForwardItfirst,ForwardItlast,constT&value){returnstd::equal_range(first,last,value,std::less{});}
template<classForwardIt,classT=typenamestd::iterator_traits<ForwardIt>::value_type,classCompare>constexprstd::pair<ForwardIt,ForwardIt>equal_range(ForwardItfirst,ForwardItlast,constT&value,Comparecomp){returnstd::make_pair(std::lower_bound(first,last,value,comp),std::upper_bound(first,last,value,comp));}Example
Run this code
#include<algorithm>#include<complex>#include<iostream>#include<vector>structS{intnumber;charname;// note: name is ignored by this comparison operatorbooloperator<(constS&s)const{returnnumber<s.number;}};structComp{booloperator()(constS&s,inti)const{returns.number<i;}booloperator()(inti,constS&s)const{returni<s.number;}};intmain(){// note: not ordered, only partitioned w.r.t. S defined belowconststd::vector<S>vec{{1,'A'},{2,'B'},{2,'C'},{2,'D'},{4,'G'},{3,'F'}};constSvalue{2,'?'};std::cout<<"Compare using S::operator<(): ";constautop=std::equal_range(vec.begin(),vec.end(),value);for(autoit=p.first;it!=p.second;++it)std::cout<<it->name<<' ';std::cout<<'\n';std::cout<<"Using heterogeneous comparison: ";constautop2=std::equal_range(vec.begin(),vec.end(),2,Comp{});for(autoit=p2.first;it!=p2.second;++it)std::cout<<it->name<<' ';std::cout<<'\n';usingCD=std::complex<double>;std::vector<CD>nums{{1,0},{2,2},{2,1},{3,0},{3,1}};autocmpz=[](CDx,CDy){returnx.real()<y.real();};#ifdef __cpp_lib_algorithm_default_value_typeautop3=std::equal_range(nums.cbegin(),nums.cend(),{2,0},cmpz);#elseautop3=std::equal_range(nums.cbegin(),nums.cend(),CD{2,0},cmpz);#endiffor(autoit=p3.first;it!=p3.second;++it)std::cout<<*it<<' ';std::cout<<'\n';}Output:
Compare using S::operator<(): B C D Using heterogeneous comparison: B C D (2,2) (2, 1) Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 Compare was required to satisfy
and T was required
to be
(strict weak ordering required) only a partitioning is required;
heterogeneous comparisons permitted
C++98 at most 2log2(N)+1 comparisons
were allowed, which is not implementable
corrected to 2log2(N)+𝓞(1)
Applying equal_range to a single-element range requires 2 comparisons, but at most 1 comparison is allowed by the complexity requirement.
See also
(C++20)
finds the range of elements matching the given value using binary search
(algorithm function object)
finds the first element not less than the given value using binary search
(function template & algorithm function object)
(C++20)
finds the first element greater than the given value using binary search
(function template & algorithm function object)
(C++20)
determines if an element exists in a range using binary search
(function template & algorithm function object)
(C++20)
divides a range of elements into two groups
(function template & algorithm function object)
(C++20)
determines if two sets of elements are the same
(function template & algorithm function object)
(C++20)
returns range of elements matching a specific key
(public member function of std::set<Key,Compare,Allocator>)
returns range of elements matching a specific key
(public member function of std::multiset<Key,Compare,Allocator>)