std::equal_range(3)
π NAME π
std::equal_range(3) β C++ Standard Library, bestie. And yes, the name is literally std::equal_range (iconic, no notes). It's the algorithm that hands you the whole equivalence range in one go. Periodt.
β¨ Synopsis β¨
Defined in header <algorithm>. Here's the signature tea, piping hot π₯:
Defined in header <algorithm>
// (1) no comparator
template< class ForwardIt, class T >
std::pair<ForwardIt, ForwardIt>
equal_range( ForwardIt first, ForwardIt last, const T& value );
(constexpr since C++20)
(until C++26)
template< class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type >
constexpr std::pair<ForwardIt, ForwardIt>
equal_range( ForwardIt first, ForwardIt last, const T& value );
(since C++26)
// (2) with comparator
template< class ForwardIt, class T, class Compare >
std::pair<ForwardIt, ForwardIt>
equal_range( ForwardIt first, ForwardIt last, const T& value, Compare comp );
(constexpr since C++20)
(until C++26)
template< class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type, class Compare >
constexpr std::pair<ForwardIt, ForwardIt>
equal_range( ForwardIt first, ForwardIt last, const T& value, Compare comp );
(since C++26)
Okay so what's the vibe? It returns a range containing all elements equivalent to value in the partitioned range [first, last). It's giving "find all my twins" energy β¨.
1) The equivalence is checked using operator<:
Returns the results of std::lower_bound(first, last, value) and std::upper_bound(first, last, value).
If any of the following conditions is satisfied, the behavior is undefined: (until C++20)
- For any element
elemof[first, last),bool(elem < value)does not imply!bool(value < elem). - The elements
elemof[first, last)are not partitioned with respect to expressionsbool(elem < value)and!bool(value < elem).
Equivalent to std::equal_range(first, last, value, std::less{}). (since C++20)
2) The equivalence is checked using comp:
Returns the results of std::lower_bound(first, last, value, comp) and std::upper_bound(first, last, value, comp).
If any of the following conditions is satisfied, the behavior is undefined:
- For any element
elemof[first, last),bool(comp(elem, value))does not imply!bool(comp(value, elem)). - The elements
elemof[first, last)are not partitioned with respect to expressionsbool(comp(elem, value))and!bool(comp(value, elem)).
π Parameters π
first, lastβ the partitioned range of elements to examine.valueβ value to compare the elements to.compβ binary predicate which returnstrueif the first argument is ordered before the second.
The signature of the predicate function should be equivalent to the following:
bool pred(const Type1 &a, const Type2 &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 value category (thus, Type1 & is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (since C++11)).
The types Type1 and Type2 must be such that an object of type T can be implicitly converted to both Type1 and Type2, and an object of type ForwardIt can be dereferenced and then implicitly converted to both Type1 and Type2.
π¦ Type requirements π¦
ForwardItmust meet the requirements of LegacyForwardIterator.Comparemust meet the requirements of BinaryPredicate. It is not required to satisfy Compare.
πΈ Return value πΈ
A std::pair containing a pair of iterators, where:
firstis an iterator to the first element of the range[first, last)not ordered beforevalue(orlastif no such element is found), andsecondis an iterator to the first element of the range[first, last)ordered aftervalue(orlastif no such element is found).
π Complexity π
Given N as std::distance(first, last):
1) At most 2 log_2(N) + O(1) comparisons with value using operator< (until C++20) / std::less{} (since C++20).
2) At most 2 log_2(N) + O(1) applications of the comparator comp.
However, if ForwardIt is not a LegacyRandomAccessIterator, the number of iterator increments is linear in N. Notably, std::set and std::multiset iterators are not random access, and so their member functions std::set::equal_range (resp. std::multiset::equal_range) should be preferred.
π«Ά Notes π«Ά
Although std::equal_range only requires [first, last) to be partitioned, this algorithm is usually used in the case where [first, last) is sorted, so that the binary search is valid for any value.
On top of the requirements of std::lower_bound and std::upper_bound, 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 std::lower_bound and std::upper_bound. For example, the result of the std::lower_bound call can be used as the argument of first in the std::upper_bound call.
Feature-test macro tea β:
__cpp_lib_algorithm_default_value_typeβ value202403, Std(C++26), Feature: List-initialization for algorithms(1,2).
π₯ Possible implementation π₯
equal_range (1)
template<class ForwardIt,
class T = typename std::iterator_traits<ForwardIt>::value_type>
constexpr std::pair<ForwardIt, ForwardIt>
equal_range(ForwardIt first, ForwardIt last, const T& value)
{
return std::equal_range(first, last, value, std::less{});
}
equal_range (2)
template<class ForwardIt,
class T = typename std::iterator_traits<ForwardIt>::value_type,
class Compare>
constexpr std::pair<ForwardIt, ForwardIt>
equal_range(ForwardIt first, ForwardIt last, const T& value, Compare comp)
{
return std::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>
struct S
{
int number;
char name;
// note: name is ignored by this comparison operator
bool operator<(const S& s) const { return number < s.number; }
};
struct Comp
{
bool operator()(const S& s, int i) const { return s.number < i; }
bool operator()(int i, const S& s) const { return i < s.number; }
};
int main()
{
// note: not ordered, only partitioned w.r.t. S defined below
const std::vector<S> vec{{1, 'A'}, {2, 'B'}, {2, 'C'},
{2, 'D'}, {4, 'G'}, {3, 'F'}};
const S value{2, '?'};
std::cout << "Compare using S::operator<(): ";
const auto p = std::equal_range(vec.begin(), vec.end(), value);
for (auto it = p.first; it != p.second; ++it)
std::cout << it->name << ' ';
std::cout << '\n';
std::cout << "Using heterogeneous comparison: ";
const auto p2 = std::equal_range(vec.begin(), vec.end(), 2, Comp{});
for (auto it = p2.first; it != p2.second; ++it)
std::cout << it->name << ' ';
std::cout << '\n';
using CD = std::complex<double>;
std::vector<CD> nums{{1, 0}, {2, 2}, {2, 1}, {3, 0}, {3, 1}};
auto cmpz = [](CD x, CD y) { return x.real() < y.real(); };
#ifdef __cpp_lib_algorithm_default_value_type
auto p3 = std::equal_range(nums.cbegin(), nums.cend(), {2, 0}, cmpz);
#else
auto p3 = std::equal_range(nums.cbegin(), nums.cend(), CD{2, 0}, cmpz);
#endif
for (auto it = 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. So yes, the tea was retroactive π
- DR
LWG 270β Applied to:C++98. Behavior as published:Comparewas required to satisfyCompareandTwas required to be LessThanComparable (strict weak ordering required). Correct behavior: only a partitioning is required; heterogeneous comparisons permitted. - DR
LWG 384β Applied to:C++98. Behavior as published: at most2 log_2(N)+1comparisons were allowed, which is not implementable^[1]. Correct behavior: corrected to2 log_2(N)+O(1).
- β Applying
equal_rangeto a single-element range requires 2 comparisons, but at most 1 comparison is allowed by the complexity requirement.
β¨ SEE ALSO β¨
okay bestie, these are the cross-reference besties β the little links that live at the bottom of the page and lowkey hold the whole friend group together π«Ά
lower_boundβ returns an iterator to the first element not less than the given value (function template) β¨upper_boundβ returns an iterator to the first element greater than a certain value (function template) πbinary_searchβ determines if an element exists in a partially-ordered range (function template) π₯partitionβ divides a range of elements into two groups (function template) πequalβ determines if two sets of elements are the same (function template) πΈequal_rangeβ returns range of elements matching a specific key (public member function ofstd::set<Key,Compare,Allocator>) πequal_rangeβ returns range of elements matching a specific key (public member function ofstd::multiset<Key,Compare,Allocator>) πranges::equal_rangeβ returns range of elements matching a specific key (C++20) (niebloid) π
mwah π thatβs the whole page, bestie! source: http://cppreference.com, version/date: 2024.06.10 β now go forth and slay your range queries β¨π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms