unordered_set删除
时间: 2023-11-03 15:04:46 浏览: 40
unordered_set可以使用erase()函数来删除元素。你可以使用元素的值或迭代器作为参数来指定要删除的元素。如果你想通过值来删除元素,可以使用find()函数来查找元素的迭代器,然后将该迭代器传递给erase()函数。以下是一个示例:
```cpp
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet = {1, 2, 3, 4, 5};
// 删除元素2
auto it = mySet.find(2);
if (it != mySet.end()) {
mySet.erase(it);
}
// 打印剩余的元素
for (const auto& num : mySet) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果将是:1 3 4 5。
在这个示例中,我们创建了一个包含整数的unordered_set。然后,我们使用find()函数查找元素2的迭代器,并将其传递给erase()函数来删除该元素。最后,我们遍历剩余的元素并打印它们。
相关问题
unordered_map或unordered_set
unordered_map或unordered_set是C++中的关联式容器,用于存储键值对或唯一的元素。unordered_map是存储<key, value>键值对的容器,而unordered_set是存储唯一元素的容器。在unordered_map中,键值对通常用于唯一地标识元素,而映射值是与键关联的对象。unordered_map和unordered_set的底层实现使用了哈希表,通过哈希函数将键映射到桶中,以实现快速的插入、查找和删除操作。与红黑树结构的关联式容器相比,unordered_map和unordered_set在查询时具有更高的效率,但在遍历元素子集时效率较低。unordered_map和unordered_set提供了一系列的接口,包括构造函数、容量函数、迭代器和元素访问函数,以便于使用和操作容器中的元素。\[2\]\[3\]
#### 引用[.reference_title]
- *1* *2* *3* [C++ unordered_map和unordered_set的使用](https://blog.csdn.net/qq_61635026/article/details/126857258)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
unordered_map、unordered_set
unordered_map和unordered_set都是C++ STL中的关联容器,它们的底层实现都是哈希表。其中,unordered_map用于存储键值对,而unordered_set则只存储键。
使用哈希表的优点是可以在O(1)的时间复杂度内进行插入、查找和删除操作,但是在最坏情况下,哈希表的时间复杂度会退化到O(n)。
unordered_map和unordered_set的使用方法与其他STL容器类似,可以使用迭代器进行遍历,也可以使用auto关键字进行类型推导。
下面是一个使用unordered_map的例子:
```c++
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, int> umap;
umap["apple"] = 1;
umap["banana"] = 2;
umap["orange"] = 3;
for (auto it = umap.begin(); it != umap.end(); it++) {
cout << it->first << ": " << it->second << endl;
}
return 0;
}
```
输出结果为:
```
orange: 3
banana: 2
apple: 1
```
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)