公有成員函式
<unordered_set>

std:: unordered_set::find

      iterator find ( const key_type& k );const_iterator find ( const key_type& k ) const;
獲取元素迭代器
在容器中搜索值為 k 的元素,如果找到則返回指向它的迭代器,否則返回指向 unordered_set::end (指向容器末尾之後) 的迭代器。

另一個成員函式 unordered_set::count 可用於僅檢查特定元素是否存在。

unordered_set 中的所有迭代器都對元素具有 const 訪問許可權(即使那些型別未以const_): 元素可以被插入或移除,但在容器中時不能被修改。

引數

k
要搜尋的鍵。
成員型別key_type是容器中元素的型別。在 unordered_set 容器中,它與value_type相同,定義為類模板引數的別名().

返回值

如果找到了指定的鍵,則返回指向該元素的迭代器;否則,如果容器中未找到該鍵,則返回指向 unordered_set::end 的迭代器。

成員型別iteratorconst_iterator都是 前向迭代器 型別。兩者可能是同一迭代器型別的別名。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// unordered_set::find
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_set<std::string> myset = { "red","green","blue" };

  std::string input;
  std::cout << "color? ";
  getline (std::cin,input);

  std::unordered_set<std::string>::const_iterator got = myset.find (input);

  if ( got == myset.end() )
    std::cout << "not found in myset";
  else
    std::cout << *got << " is in myset";

  std::cout << std::endl;

  return 0;
}

可能的輸出
color? blue
blue is in myset


複雜度

平均情況:常量。
最壞情況:與 容器大小成線性關係。

迭代器有效性

沒有變化。

另見