c+++unordered_map
时间: 2023-11-24 11:51:55 浏览: 34
C++中的unordered_map是一种关联容器,它使用哈希表来存储键值对。与map相比,unordered_map的查找速度更快,因为它的查找时间是常量级别的。但是,unordered_map的建立比较耗费时间。此外,与map不同,unordered_map的遍历顺序与创建该容器时输入元素的顺序是不一定一致的,遍历是按照哈希表从前往后依次遍历的。
下面是一个简单的unordered_map的例子:
```c++
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, int> myMap = {
{"apple", 1},
{"banana", 2},
{"orange", 3}
};
std::cout << "The value of apple is: " << myMap["apple"] << std::endl;
std::cout << "The value of banana is: " << myMap["banana"] << std::endl;
std::cout << "The value of orange is: " << myMap["orange"] << std::endl;
return 0;}
```
输出结果为:
```
The value of apple is: 1
The value of banana is: 2
The value of orange is: 3
```