Namespaces
Variants

std::map<Key,T,Compare,Allocator>:: equal_range

From cppreference.net

std:: pair < iterator, iterator > equal_range ( const Key & key ) ;
(1) (constexpr since C++26)
std:: pair < const_iterator, const_iterator >
equal_range ( const Key & key ) const ;
(2) (constexpr since C++26)
template < class K >
std:: pair < iterator, iterator > equal_range ( const K & x ) ;
(3) (since C++14)
(constexpr since C++26)
template < class K >

std:: pair < const_iterator, const_iterator >

equal_range ( const K & x ) const ;
(4) (since C++14)
(constexpr since C++26)

指定されたキーを持つすべての要素を含む範囲を返します。この範囲は2つのイテレータによって定義され、1つは指定されたキーより 小さくない 最初の要素を指し、もう1つは指定されたキーより大きい最初の要素を指します。

あるいは、最初のイテレータは lower_bound() で取得でき、2番目のイテレータは upper_bound() で取得できます。

1,2) キーを key と比較します。
3,4) キーを値 x と比較します。
このオーバーロードは、 Compare transparent である場合にのみ、オーバーロード解決に参加します。これにより、 Key のインスタンスを構築せずにこの関数を呼び出すことが可能になります。

目次

パラメータ

key - 要素を比較するキー値
x - Key と比較可能な代替値

戻り値

std::pair 必要な範囲を定義する一組のイテレータを含む:

  • 最初のイテレータは、指定されたキー以上である最初の要素を指し、そのような要素が存在しない場合は end ( ) を指します。
  • 2番目のイテレータは、指定されたキーより大きい最初の要素を指し、そのような要素が存在しない場合は end ( ) を指します。

計算量

コンテナのサイズに対して対数的。

注記

機能テスト マクロ 標準 機能
__cpp_lib_generic_associative_lookup 201304L (C++14) 連想コンテナにおける 異種比較ルックアップ 、オーバーロード ( 3,4 )

#include <iostream>
#include <map>
int main()
{
    const std::map<int, const char*> m
    {
        {0, "zero"},
        {1, "one"},
        {2, "two"}
    };
    auto p = m.equal_range(1);
    for (auto& q = p.first; q != p.second; ++q)
        std::cout << "m[" << q->first << "] = " << q->second << '\n';
    if (p.second == m.find(2))
        std::cout << "end of equal_range (p.second) is one-past p.first\n";
    else
        std::cout << "unexpected; p.second expected to be one-past p.first\n";
    auto pp = m.equal_range(-1);
    if (pp.first == m.begin())
        std::cout << "pp.first is iterator to first not-less than -1\n";
    else
        std::cout << "unexpected pp.first\n";
    if (pp.second == m.begin())
        std::cout << "pp.second is iterator to first element greater-than -1\n";
    else
        std::cout << "unexpected pp.second\n";
    auto ppp = m.equal_range(3);
    if (ppp.first == m.end())
        std::cout << "ppp.first is iterator to first not-less than 3\n";
    else
        std::cout << "unexpected ppp.first\n";
    if (ppp.second == m.end())
        std::cout << "ppp.second is iterator to first element greater-than 3\n";
    else
        std::cout << "unexpected ppp.second\n";
}

出力:

m[1] = one
end of equal_range (p.second) is one-past p.first
pp.first is iterator to first not-less than -1
pp.second is iterator to first element greater-than -1
ppp.first is iterator to first not-less than 3
ppp.second is iterator to first element greater-than 3

関連項目

特定のキーを持つ要素を検索
(公開メンバ関数)
(C++20)
コンテナが特定のキーを持つ要素を含むかどうかをチェック
(公開メンバ関数)
特定のキーに一致する要素の数を返す
(公開メンバ関数)
指定されたキーより 大きい 最初の要素へのイテレータを返す
(公開メンバ関数)
指定されたキーより 小さくない 最初の要素へのイテレータを返す
(公開メンバ関数)
特定のキーに一致する要素の範囲を返す
(関数テンプレート)