名前空間
変種
操作

std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::equal_range

From cppreference.com
 
 
 
 
std::pair<iterator, iterator> equal_range( const Key& key );
(1) (C++23から)
std::pair<const_iterator, const_iterator> equal_range( const Key& key ) const;
(2) (C++23から)
template< class K >
std::pair<iterator, iterator> equal_range( const K& x );
(3) (C++23から)
template< class K >
std::pair<const_iterator, const_iterator> equal_range( const K& x ) const;
(4) (C++23から)

指定されたキーを持つコンテナ内のすべての要素を含む範囲を返します。この範囲は、2つのイテレータによって定義されます。1つは keyそれ以下 である最初の要素を指し、もう1つは key より 大きい 最初の要素を指します。あるいは、最初のイテレータは lower_bound() で取得でき、2番目のイテレータは upper_bound() で取得できます。

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

目次

[編集] パラメータ

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

[編集] 戻り値

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

keyそれ以下 である要素がない場合、最初の要素として終了後 (past-the-end) ( end() 参照) のイテレータが返されます。同様に、 key より 大きい 要素がない場合、2番目の要素として終了後 (past-the-end) のイテレータが返されます。

[編集] 計算量

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

[編集]

#include <iostream>
#include <flat_map>
 
int main()
{
    const std::flat_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

[編集] 関連項目

特定のキーを持つ要素を検索する
(公開メンバ関数) [編集]
コンテナが特定のキーを持つ要素を含むか確認する
(公開メンバ関数) [編集]
特定のキーに一致する要素の数を返す
(公開メンバ関数) [編集]
指定されたキーより大きい最初の要素へのイテレータを返す
(公開メンバ関数) [編集]
指定されたキーより小さくない最初の要素へのイテレータを返す
(公開メンバ関数) [編集]
特定のキーに一致する要素の範囲を返す
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)