C++中unordered_set是什么?
时间: 2024-05-04 09:13:23 浏览: 13
C++中的unordered_set是一种无序的容器,它存储唯一的元素,并且可以快速地进行插入、删除和查找操作。与set相比,unordered_set的元素是无序的,但是查找效率非常高[^1]。
下面是一个使用unordered_set的简单示例代码:
```cpp
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet;
// 插入元素
mySet.insert(10);
mySet.insert(20);
mySet.insert(30);
// 查找元素
if (mySet.find(20) != mySet.end()) {
std::cout << "元素20存在于unordered_set中" << std::endl;
}
// 删除元素
mySet.erase(30);
// 遍历元素
for (const auto& element : mySet) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果:
```
元素20存在于unordered_set中
10 20
```