名前空間
変種
操作

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

From cppreference.com
< cpp‎ | コンテナ‎ | map
 
 
 
 
std::pair<iterator, iterator> equal_range( const Key& key );
(1)
std::pair<const_iterator, const_iterator> equal_range( const Key& key ) const;
(2)
template< class K >
std::pair<iterator, iterator> equal_range( const K& x );
(3) (C++14以降)
template< class K >
std::pair<const_iterator, const_iterator> equal_range( const K& x ) const;
(4) (C++14以降)

コンテナ内で指定されたキーを持つすべての要素を含む範囲を返します。この範囲は、最初の要素を指すイテレータと、key より大きい最初の要素を指すイテレータによって定義されます。前者は lower_bound() を使用して取得でき、後者は upper_bound() を使用して取得できます。

1,2) キーと key を比較します。
3,4) キーと値 x を比較します。このオーバーロードは、修飾子 Compare::is_transparent が有効で型を表す場合にのみオーバーロード解決に参加します。これにより、Key のインスタンスを構築せずにこの関数を呼び出すことができます。

目次

[編集] パラメータ

key - 比較対象の値
x - Key と比較可能な代替値

[編集] 戻り値

std::pair。目的の範囲を定義するイテレータのペアを含みます。最初のイテレータは keyそれ以下 である最初の要素を指し、2番目のイテレータは key より 大きい 最初の要素を指します。

もし key 以下の要素が存在しない場合、最初の要素として末尾を過ぎた(end() 参照)イテレータが返されます。同様に、key より大きい要素が存在しない場合、2番目の要素として末尾を過ぎたイテレータが返されます。

[編集] 計算量

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

注釈

機能テストマクロ 規格 機能
__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)
コンテナが特定のキーを持つ要素を含むか確認する
(公開メンバ関数) [編集]
特定のキーに一致する要素の数を返す
(公開メンバ関数) [編集]
指定されたキーより大きい最初の要素へのイテレータを返す
(公開メンバ関数) [編集]
指定されたキーより小さくない最初の要素へのイテレータを返す
(公開メンバ関数) [編集]
特定のキーに一致する要素の範囲を返す
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)