std::map<Key,T,Compare,Allocator>::find
提供: cppreference.com
iterator find( const Key& key ); |
(1) | |
const_iterator find( const Key& key ) const; |
(2) | |
template< class K > iterator find( const K& x ); |
(3) | (C++14以上) |
template< class K > const_iterator find( const K& x ) const; |
(4) | (C++14以上) |
1,2) キー
key
と同等なキーを持つ要素を探します。 3,4) 値
x
と比較して同等なキーを持つ要素を探します。 このオーバーロードは、修飾識別子 Compare::is_transparent が有効で、それが型を表す場合にのみ、オーバーロード解決に参加します。 これにより Key
のインスタンスを構築せずにこの関数を呼ぶことが可能となります。目次 |
[編集] 引数
key | - | 検索する要素のキーの値 |
x | - | キーと透過的に比較可能な任意の型の値 |
[編集] 戻り値
key
と同等なキーを持つ要素を指すイテレータ。 そのような要素が見つからなければ、終端イテレータ (end() を参照) が返されます。
[編集] 計算量
コンテナのサイズの対数。
[編集] 例
Run this code
#include <iostream> #include <map> struct FatKey { int x; int data[1000]; }; struct LightKey { int x; }; bool operator<(const FatKey& fk, const LightKey& lk) { return fk.x < lk.x; } bool operator<(const LightKey& lk, const FatKey& fk) { return lk.x < fk.x; } bool operator<(const FatKey& fk1, const FatKey& fk2) { return fk1.x < fk2.x; } int main() { // 単純な比較のデモ std::map<int,char> example = {{1,'a'},{2,'b'}}; auto search = example.find(2); if (search != example.end()) { std::cout << "Found " << search->first << " " << search->second << '\n'; } else { std::cout << "Not found\n"; } // 透過的な比較のデモ std::map<FatKey, char, std::less<>> example2 = { { {1, {} },'a'}, { {2, {} },'b'} }; LightKey lk = {2}; auto search2 = example2.find(lk); if (search2 != example2.end()) { std::cout << "Found " << search2->first.x << " " << search2->second << '\n'; } else { std::cout << "Not found\n"; } }
出力:
Found 2 b Found 2 b
[編集] 関連項目
指定されたキーと一致する要素の数を返します (パブリックメンバ関数) | |
指定されたキーに一致する要素の範囲を返します (パブリックメンバ関数) |