std::map<Key,T,Compare,Allocator>:: equal_range
From ja.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
>
|
(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
のインスタンスを構築せずにこの関数を呼び出すことが可能になります。
目次パラメータ
戻り値std::pair 必要な範囲を定義する一組のイテレータを含む:
計算量コンテナのサイズに対して対数的。 注記
例
このコードを実行
#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 関連項目
|