std::unordered_set<Key,Hash,KeyEqual,Allocator>:: find
From cppreference.net
<
cpp
|
container
|
unordered set
|
iterator find
(
const
Key
&
key
)
;
|
(1) |
(C++11以降)
(constexpr C++26以降) |
|
const_iterator find
(
const
Key
&
key
)
const
;
|
(2) |
(C++11以降)
(constexpr C++26以降) |
|
template
<
class
K
>
iterator find ( const K & x ) ; |
(3) |
(C++20以降)
(constexpr C++26以降) |
|
template
<
class
K
>
const_iterator find ( const K & x ) const ; |
(4) |
(C++20以降)
(constexpr C++26以降) |
1,2)
キーが
key
と等しい要素を検索します。
3,4)
キーが
x
と等価比較される要素を検索します。
このオーバーロードは、
Hash
と
KeyEqual
の両方が
transparent
である場合にのみ、オーバーロード解決に参加します。これは、そのような
Hash
が
K
型と
Key
型の両方で呼び出し可能であり、かつ
KeyEqual
がtransparentであることを前提としています。これらが組み合わさることで、
Key
のインスタンスを構築することなくこの関数を呼び出すことが可能になります。
目次 |
パラメータ
| key | - | 検索する要素のキー値 |
| x | - | キーと透過的に比較可能な任意の型の値 |
戻り値
要求された要素へのイテレータ。そのような要素が見つからない場合は、終端( end() )イテレータが返されます。
計算量
平均的には定数時間、最悪ケースではコンテナのサイズに対して線形時間。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_generic_unordered_lookup
|
201811L
|
(C++20) | 非順序連想コンテナにおける 異種比較ルックアップ ; オーバーロード ( 3,4 ) |
例
このコードを実行
#include <cstddef> #include <functional> #include <iostream> #include <source_location> #include <string> #include <string_view> #include <unordered_set> using namespace std::literals; namespace logger { bool enabled{false}; } inline void who(const std::source_location sloc = std::source_location::current()) { if (logger::enabled) std::cout << sloc.function_name() << '\n'; } struct string_hash // C++20の透過的ハッシュ { using hash_type = std::hash<std::string_view>; using is_transparent = void; std::size_t operator()(const char* str) const { who(); return hash_type{}(str); } std::size_t operator()(std::string_view str) const { who(); return hash_type{}(str); } std::size_t operator()(const std::string& str) const { who(); return hash_type{}(str); } }; int main() { std::unordered_set<int> example{1, 2, -10}; std::cout << "単純な比較デモ:\n" << std::boolalpha; if (auto search = example.find(2); search != example.end()) std::cout << "見つかりました " << *search << '\n'; else std::cout << "見つかりませんでした\n"; std::unordered_set<std::string, string_hash, std::equal_to<>> set{"one"s, "two"s}; logger::enabled = true; std::cout << "非順序コンテナの異種型ルックアップ(透過的ハッシュ):\n" << (set.find("one") != set.end()) << '\n' << (set.find("one"s) != set.end()) << '\n' << (set.find("one"sv) != set.end()) << '\n'; }
出力例:
単純な比較デモ: 見つかりました 2 非順序コンテナの異種型ルックアップ(透過的ハッシュ): std::size_t string_hash::operator()(const char*) const true std::size_t string_hash::operator()(const std::string&) const true std::size_t string_hash::operator()(std::string_view) const true
関連項目
|
指定されたキーに一致する要素の数を返す
(public member function) |
|
|
指定されたキーに一致する要素の範囲を返す
(public member function) |