std::flat_set<Key,Compare,KeyContainer>::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 <flat_set> #include <functional> #include <print> #include <ranges> #include <string> #include <string_view> #include <tuple> struct Names { std::string forename, surname; friend auto operator<(const Names& lhs, const Names& rhs) { return std::tie(lhs.surname, lhs.forename) < std::tie(rhs.surname, rhs.forename); } }; struct SurnameCompare { std::string_view surname; friend bool operator<(const Names& lhs, const SurnameCompare& rhs) { return lhs.surname < rhs.surname; } friend bool operator<(const SurnameCompare& lhs, const Names& rhs) { return lhs.surname < rhs.surname; } }; std::set<Names, std::less<>> characters { {"Homer", "Simpson"}, {"Marge", "Simpson"}, {"Lisa", "Simpson"}, {"Ned", "Flanders"}, {"Joe", "Quimby"} }; void print_unique(const Names& names) { auto [begin, end] = characters.equal_range(names); std::print( "Found {} characters with name \"{} {}\"\n", std::distance(begin, end), names.forename, names.surname ); } void print_by_surname(std::string_view surname) { auto [begin, end] = characters.equal_range(SurnameCompare{surname}); std::print("Found {} characters with surname \"{}\":\n", std::distance(begin, end), surname); for (const Names& names : std::ranges::subrange(begin, end)) std::print(" {} {}\n", names.forename, names.surname); } int main() { print_unique({"Maude", "Flanders"}); print_unique({"Lisa", "Simpson"}); print_by_surname("Simpson"); }
出力
Found 0 characters with name "Maude Flanders"
Found 1 characters with name "Lisa Simpson"
Found 3 characters with surname "Simpson":
Homer Simpson
Lisa Simpson
Marge Simpson[編集] 関連項目
| 特定のキーを持つ要素を検索する (公開メンバ関数) | |
| コンテナが特定のキーを持つ要素を含むか確認する (公開メンバ関数) | |
| 特定のキーに一致する要素の数を返す (公開メンバ関数) | |
| 指定されたキーより大きい最初の要素へのイテレータを返す (公開メンバ関数) | |
| 指定されたキーより小さくない最初の要素へのイテレータを返す (公開メンバ関数) | |
| 特定のキーに一致する要素の範囲を返す (関数テンプレート) |