unordered_set.find
时间: 2023-08-26 09:16:59 浏览: 186
stl_code.rar_STL vector_hash_stl set code_vector_vector stl
unordered_set.find()函数是用于在unordered_set容器中搜索指定元素的C++ STL内置函数。它返回一个迭代器,指向找到的元素。如果找不到指定元素,则返回指向unordered_set的end()迭代器。该函数的时间复杂度是平均O(1)。下面是一个示例代码,展示了如何使用unordered_set.find()函数来搜索元素并判断是否存在:
```cpp
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;
int main() {
unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" };
if (sampleSet.find("geeks1") != sampleSet.end()) {
cout << "element found." << endl;
} else {
cout << "element not found." << endl;
}
return 0;
}
```
在这个示例中,我们创建了一个包含三个字符串的unordered_set(sampleSet)。然后,我们使用find()函数来搜索"geeks1"这个元素。如果找到了,就输出"element found.",否则输出"element not found."。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C++:哈希,unordered_map和unordered_set](https://blog.csdn.net/zhang_si_hang/article/details/126739994)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [【C++】unordered_set中find()用法及代码示例](https://blog.csdn.net/qq_29931565/article/details/124511606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文