Namespaces
Variants

std::ranges:: equal_range

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
(注:指定されたHTMLフラグメントには翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
定義済みヘッダー <algorithm>
呼び出しシグネチャ
(1)
template < std:: forward_iterator I, std:: sentinel_for < I > S,

class T, class Proj = std:: identity ,
std:: indirect_strict_weak_order
< const T * , std :: projected < I, Proj >> Comp = ranges:: less >
constexpr ranges:: subrange < I > equal_range ( I first, S last, const T & value,

Comp comp = { } , Proj proj = { } ) ;
(C++20以降)
(C++26まで)
template < std:: forward_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
class T = std :: projected_value_t < I, Proj > ,
std:: indirect_strict_weak_order
< const T * , std :: projected < I, Proj >> Comp = ranges:: less >
constexpr ranges:: subrange < I > equal_range ( I first, S last, const T & value,

Comp comp = { } , Proj proj = { } ) ;
(C++26以降)
(2)
template < ranges:: forward_range R,

class T, class Proj = std:: identity ,
std:: indirect_strict_weak_order
< const T * , std :: projected < ranges:: iterator_t < R > ,
Proj >> Comp = ranges:: less >
constexpr ranges:: borrowed_subrange_t < R >

equal_range ( R && r, const T & value, Comp comp = { } , Proj proj = { } ) ;
(C++20以降)
(C++26まで)
template < ranges:: forward_range R,

class Proj = std:: identity ,
class T = std :: projected_value_t < ranges:: iterator_t < R > , Proj > ,
std:: indirect_strict_weak_order
< const T * , std :: projected < ranges:: iterator_t < R > ,
Proj >> Comp = ranges:: less >
constexpr ranges:: borrowed_subrange_t < R >

equal_range ( R && r, const T & value, Comp comp = { } , Proj proj = { } ) ;
(C++26以降)
1) 範囲 [ first , last ) 内の value と等価なすべての要素を含むビューを返します。

範囲 [ first , last ) value に対して少なくとも部分的に順序付けられている必要があります。すなわち、以下のすべての要件を満たさなければなりません:

  • element < value または comp ( element, value ) に関して区分化されている (つまり、式が true となるすべての要素が、式が false となるすべての要素の前に位置する)。
  • ! ( value < element ) または ! comp ( value, element ) に関して区分化されている。
  • すべての要素について、 element < value または comp ( element, value ) true の場合、 ! ( value < element ) または ! comp ( value, element ) もまた true となる。

完全にソートされた範囲は以下の基準を満たします。

返されるビューは2つのイテレータから構築され、1つは value 以上である最初の要素を指し、もう1つは value より大きい最初の要素を指します。最初のイテレータは std::ranges::lower_bound() で、2番目のイテレータは std::ranges::upper_bound() でそれぞれ取得することができます。

2) (1) と同様ですが、ソース範囲として r を使用します。これは、範囲 ranges:: begin ( r ) first として、 ranges:: end ( r ) last として使用する場合と同様です。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - その他のテキスト(Parameters、Return value、Complexityなど)はC++関連の専門用語として翻訳せず、原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどはすべて変更せず保持しました - 番号部分(tocnumber)もそのまま保持しました - リンク先のURLも変更していません

パラメータ

first, last - 検査対象の要素の 範囲 を定義するイテレータ-番兵ペア
r - 検査対象の要素の範囲
value - 要素と比較する値
comp - 第1引数が第2引数より 小さい 場合(つまり順序付けにおいて前に来る場合)
proj - 要素に適用する射影

戻り値

std::ranges::subrange 望ましい範囲を定義するイテレータのペアを含む、最初の要素は value より 小さくない 最初の要素を指し、2番目の要素は value より 大きい 最初の要素を指す。

value 以上の要素が存在しない場合、最後のイテレータ( last または ranges:: end ( r ) に等しいイテレータ)が最初の要素として返されます。同様に、 value 超える 要素が存在しない場合、最後のイテレータが2番目の要素として返されます。

計算量

比較回数は、 first last の間の距離に対して対数的です(最大 2 * log 2 (last - first) + O(1) 回の比較)。しかし、 random_access_iterator をモデル化しないイテレータの場合、イテレータのインクリメント回数は線形になります。

実装例

struct equal_range_fn
{
    template<std::forward_iterator I, std::sentinel_for<I> S,
             class Proj = std::identity, class T = std::projected_value_t<I, Proj>,
             std::indirect_strict_weak_order
                 <const T*, std::projected<I, Proj>> Comp = ranges::less>
    constexpr ranges::subrange<I>
        operator()(I first, S last, const T& value, Comp comp = {}, Proj proj = {}) const
    {
        return ranges::subrange
        (
            ranges::lower_bound(first, last, value, std::ref(comp), std::ref(proj)),
            ranges::upper_bound(first, last, value, std::ref(comp), std::ref(proj))
        );
    }
    template<ranges::forward_range R, class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>,
             std::indirect_strict_weak_order
                 <const T*, std::projected<ranges::iterator_t<R>,
                                           Proj>> Comp = ranges::less>
    constexpr ranges::borrowed_subrange_t<R>
        operator()(R&& r, const T& value, Comp comp = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value,
                       std::ref(comp), std::ref(proj));
    }
};
inline constexpr equal_range_fn equal_range;

注記

機能テスト マクロ 標準 機能
__cpp_lib_algorithm_default_value_type 202403 (C++26) リスト初期化 アルゴリズム用 ( 1,2 )

#include <algorithm>
#include <compare>
#include <complex>
#include <iostream>
#include <vector>
struct S
{
    int number {};
    char name {};
    // 注: これらの比較演算子ではnameは無視される
    friend bool operator== (const S s1, const S s2) { return s1.number == s2.number; }
    friend auto operator<=>(const S s1, const S s2) { return s1.number <=> s2.number; }
    friend std::ostream& operator<<(std::ostream& os, S o)
    {
        return os << '{' << o.number << ", '" << o.name << "'}";
    }
};
void println(auto rem, const auto& v)
{
    for (std::cout << rem; const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    // 注: 順序付けされておらず、以下のS定義に関してのみ分割されている
    std::vector<S> vec
    {
        {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4, 'D'}, {4,'G'}, {3,'F'}
    };
    const S value{2, '?'};
    namespace ranges = std::ranges;
    auto a = ranges::equal_range(vec, value);
    println("1. ", a);
    auto b = ranges::equal_range(vec.begin(), vec.end(), value);
    println("2. ", b);
    auto c = ranges::equal_range(vec, 'D', ranges::less {}, &S::name);
    println("3. ", c);
    auto d = ranges::equal_range(vec.begin(), vec.end(), 'D', ranges::less {}, &S::name);
    println("4. ", d);
    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 = ranges::equal_range(nums, {2, 0}, cmpz);
    #else
        auto p3 = ranges::equal_range(nums, CD{2, 0}, cmpz);
    #endif
    println("5. ", p3);
}

出力:

1. {2, 'B'} {2, 'C'} {2, 'D'}
2. {2, 'B'} {2, 'C'} {2, 'D'}
3. {2, 'D'} {4, 'D'}
4. {2, 'D'} {4, 'D'}
5. (2,2) (2,1)

関連項目

指定された値より 小さくない 最初の要素へのイテレータを返す
(アルゴリズム関数オブジェクト)
特定の値より 大きい 最初の要素へのイテレータを返す
(アルゴリズム関数オブジェクト)
半順序範囲内に要素が存在するかどうかを判定する
(アルゴリズム関数オブジェクト)
要素の範囲を2つのグループに分割する
(アルゴリズム関数オブジェクト)
2つの要素集合が同じかどうかを判定する
(アルゴリズム関数オブジェクト)
特定のキーに一致する要素の範囲を返す
(関数テンプレート)