std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: find
From cppreference.net
<
cpp
|
container
|
unordered map
|
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 <string> #include <string_view> #include <unordered_map> using namespace std::literals; struct string_hash { using hash_type = std::hash<std::string_view>; using is_transparent = void; std::size_t operator()(const char* str) const { return hash_type{}(str); } std::size_t operator()(std::string_view str) const { return hash_type{}(str); } std::size_t operator()(const std::string& str) const { return hash_type{}(str); } }; int main() { // シンプルな比較デモ std::unordered_map<int, char> example{{1, 'a'}, {2, 'b'}}; if (auto search = example.find(2); search != example.end()) std::cout << "Found " << search->first << ' ' << search->second << '\n'; else std::cout << "Not found\n"; // C++20 デモ: 非順序連想コンテナの異種混合ルックアップ(透過的ハッシュ) std::unordered_map<std::string, size_t, string_hash, std::equal_to<>> map{{"one"s, 1}}; std::cout << std::boolalpha << (map.find("one") != map.end()) << '\n' << (map.find("one"s) != map.end()) << '\n' << (map.find("one"sv) != map.end()) << '\n'; }
出力:
Found 2 b true true true
関連項目
|
境界チェック付きで指定された要素にアクセス
(公開メンバ関数) |
|
|
指定された要素にアクセスまたは挿入
(公開メンバ関数) |
|
|
特定のキーに一致する要素の数を返す
(公開メンバ関数) |
|
|
特定のキーに一致する要素の範囲を返す
(公開メンバ関数) |