GeezerVison is showing cpprefjp.github.io — std::equal_range - cpprefjp C++日本語リファレンス
最終更新日時:
2026年08月25日 12時11分17秒 (JST)
Akira Takahashi
が更新
履歴
編集
function template
<algorithm>
std:: equal_range
namespace std {
template < class ForwardIterator ,
class T >
pair < ForwardIterator , ForwardIterator >
equal_range ( ForwardIterator first ,
ForwardIterator last ,
const T & value ); // (1) C++98
template < class ForwardIterator ,
class T >
constexpr pair < ForwardIterator , ForwardIterator >
equal_range ( ForwardIterator first ,
ForwardIterator last ,
const T & value ); // (1) C++20
template < class ForwardIterator ,
class T = typename iterator_traits < ForwardIterator >:: value_type >
constexpr pair < ForwardIterator , ForwardIterator >
equal_range ( ForwardIterator first ,
ForwardIterator last ,
const T & value ); // (1) C++26
template < class ForwardIterator ,
class T ,
class Compare >
pair < ForwardIterator , ForwardIterator >
equal_range ( ForwardIterator first ,
ForwardIterator last ,
const T & value ,
Compare comp ); // (2) C++98
template < class ForwardIterator ,
class T ,
class Compare >
constexpr pair < ForwardIterator , ForwardIterator >
equal_range ( ForwardIterator first ,
ForwardIterator last ,
const T & value ,
Compare comp ); // (2) C++20
template < class ForwardIterator ,
class T = typename iterator_traits < ForwardIterator >:: value_type ,
class Compare >
constexpr pair < ForwardIterator , ForwardIterator >
equal_range ( ForwardIterator first ,
ForwardIterator last ,
const T & value ,
Compare comp ); // (2) C++26
}
概要
lower_bound() とupper_bound() の結果を組で取得する。
要件
[first,last) の要素 e は e < value および !(value < e) 、あるいは comp(e, value) および !comp(value, e) によって区分化 されていなければならない。
また、[first, last) の要素 e は全て暗黙に、e < value が !(value < e) または comp(e, value) が !comp(value, e) を意味している必要がある。
戻り値
(1) : make_pair (lower_bound (first, last, value), upper_bound (first, last, value))
(2) : make_pair (lower_bound (first, last, value, comp), upper_bound (first, last, value, comp))
計算量
最大で 2 * log2(last - first) + O(1) 回の比較を行う
備考
(1), (2) :
C++26 : 引数として波カッコ初期化{}を受け付ける
例
基本的な使い方
出力
3
4
波カッコ初期化を入力として使用する (C++26)
#include <algorithm>
#include <iostream>
#include <vector>
struct Point {
int x ;
int y ;
bool operator == ( const Point & other ) const = default ;
auto operator <=> ( const Point & other ) const = default ;
};
int main () {
std::vector < Point > v = {
{ 1 , 2 },
{ 3 , 4 },
{ 3 , 4 },
{ 5 , 6 },
};
// 値{3, 4}が見つかる範囲を取得
auto result = std::equal_range ( v.begin() , v.end() , { 3 , 4 });
while ( result . first != result . second ) {
std::cout << result . first -> x << "," << result . first -> y << std::endl ;
++ result . first ;
}
}
出力
3,4
3,4
関連項目
参照