unordered_map实现
时间: 2023-11-07 13:05:59 浏览: 38
好的,我知道了。unordered_map是C++ STL中的一个关联容器,用于存储键值对。它的实现基于哈希表,可以实现快速的查找、插入和删除操作。
在unordered_map中,每个键值对被称为一个元素,其中键是唯一的,值可以重复。元素的插入顺序与元素在容器中的位置无关。
unordered_map的使用方法与其他STL容器类似,可以通过insert()、erase()、find()等函数来操作元素。同时,也可以使用迭代器来遍历容器中的元素。
下面是一个简单的unordered_map示例:
```c++
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> myMap;
myMap.insert({"apple", 3});
myMap.insert({"banana", 2});
myMap.insert({"orange", 4});
std::cout << "The value of apple is " << myMap["apple"] << std::endl;
auto it = myMap.find("banana");
if (it != myMap.end()) {
std::cout << "The value of banana is " << it->second << std::endl;
}
return 0;
}
```
输出结果为:
```
The value of apple is 3
The value of banana is 2
```