unordered_set容器
时间: 2023-10-15 19:26:33 浏览: 93
unordered_set是一种容器,它存储的元素没有特定的顺序,允许根据元素的值快速获取单个元素。它是一种连续内存开放式寻址/封闭式散列容器,具有类似于向量的内存分配。在unordered_set容器中搜索值为k的元素,可以使用unordered_set::find方法。该方法返回一个迭代器,如果找到了值为k的元素,则指向该元素;如果没有找到,则指向unordered_set的末尾之后的元素。以下是一个使用unordered_set::find的示例代码:
```cpp
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main() {
unordered_set<string> myset = { "red", "green", "blue" };
string input;
cout << "color? ";
getline(cin, input);
unordered_set<string>::const_iterator got = myset.find(input);
if (got == myset.end())
cout << "not found in myset";
else
cout << *got << " is in myset";
cout << endl;
return 0;
}
```
该示例代码创建了一个包含三个元素的unordered_set容器,并从用户输入中获取一个值作为搜索的关键字。然后使用unordered_set::find方法在容器中搜索该关键字,并根据搜索结果输出相应的消息。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [unordered_flat_set:C ++连续内存哈希容器。-开源](https://download.csdn.net/download/weixin_42109545/17767557)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [C++11:关联式容器:unordered_set](https://blog.csdn.net/qq_59535638/article/details/131105769)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文