std::set<Key,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 <set>
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::set<int> example = {1, 2, 3, 4};
auto search = example.find(2);
if (search != example.end()) {
std::cout << "Found " << (*search) << '\n';
} else {
std::cout << "Not found\n";
}
// 透過的な比較のデモ
std::set<FatKey, std::less<>> example2 = { {1, {} }, {2, {} }, {3, {} }, {4, {} } };
LightKey lk = {2};
auto search2 = example2.find(lk);
if (search2 != example2.end()) {
std::cout << "Found " << search2->x << '\n';
} else {
std::cout << "Not found\n";
}
}
出力:
Found 2
Found 2
関連項目
| 指定されたキーと一致する要素の数を返します (パブリックメンバ関数) | |
| 指定されたキーに一致する要素の範囲を返します (パブリックメンバ関数) |