std::map<Key,T,Compare,Allocator>::equal_range
提供: cppreference.com
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以上) |
コンテナ内の指定されたキーを持つすべての要素を含む範囲を返します。 範囲は2つのイテレータで示されます。 ひとつは key
より小さくない最初の要素を指し、もうひとつは key
より大きい最初の要素を指します。 代わりに lower_bound() を使用して最初の要素を取得し、 upper_bound() を使用して2番目の要素を取得することもできます。
1,2) キーと
key
を比較します。3,4) キーと値
x
を比較します。 このオーバーロードは、修飾識別子 Compare::is_transparent が有効で、型を表す場合にのみ、オーバーロード解決に参加します。 これにより Key
を構築せずにこの関数を呼ぶことができます。 This section is incomplete Reason: explain better |
目次 |
[編集]引数
key | - | 要素と比較するキーの値 |
x | - | Key と比較可能な代わりの値 |
[編集]戻り値
所望の範囲を示す1組のイテレータを持つ std::pair。 その最初の要素は key
より小さくない最初の要素を指し、その2番目の要素は key
より大きい最初の要素を指します。
key
より小さくない要素がない場合は、最初の要素として終端イテレータ (end() を参照) が返されます。 同様に、 key
より大きい要素がない場合は、2番目の要素として終端イテレータが返されます。
[編集]計算量
コンテナのサイズの対数。
[編集]例
Run this code
#include <map>#include <iostream> int main(){conststd::map<int, constchar*> 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
[編集]関連項目
指定されたキーを持つ要素を探します (パブリックメンバ関数) | |
指定されたキーより大きい最初の要素を指すイテレータを返します (パブリックメンバ関数) | |
指定されたキーより小さくない最初の要素を指すイテレータを返します (パブリックメンバ関数) |