c++清空unordered_map
时间: 2024-01-12 16:22:39 浏览: 28
清空unordered_map可以使用clear()函数。下面是一个示例代码:
```cpp
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, std::string> umap;
umap.insert(std::make_pair(2023, "yes"));
umap.insert(std::make_pair(2024, "no"));
umap.insert(std::make_pair(2025, "before"));
umap.insert(std::make_pair(2026, "now"));
std::cout << "Before clear: " << std::endl;
for (auto& e : umap) {
std::cout << e.first << "=>" << e.second << " ";
}
std::cout << std::endl;
umap.clear();
std::cout << "After clear: " << std::endl;
for (auto& e : umap) {
std::cout << e.first << "=>" << e.second << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果:
```
Before clear:
2023=>yes 2024=>no 2025=>before 2026=>now
After clear:
```
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)