C++unordered_map at
时间: 2023-10-20 17:32:12 浏览: 125
C++中的`unordered_map`是一个哈希表(hash table)实现的关联容器,用于存储键值对。你可以使用`at()`函数来访问`unordered_map`中特定的键所对应的值。
例如,假设你有一个`unordered_map`对象`myMap`,其中存储了一些整数键和对应的字符串值。你可以使用以下方式来访问特定键的值:
```cpp
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, std::string> myMap;
myMap[1] = "apple";
myMap[2] = "banana";
myMap[3] = "orange";
// 使用at()函数访问特定键的值
std::cout << myMap.at(2) << std::endl;
return 0;
}
```
在上面的例子中,`myMap.at(2)`将返回键为`2`的值,即输出为`"banana"`。
需要注意的是,如果指定的键不存在于`unordered_map`中,使用`at()`函数将会引发`std::out_of_range`异常。因此,在使用`at()`函数之前,最好先检查指定的键是否存在,可以使用`count()`函数或者使用迭代器来判断。
相关问题
C++unordered_map函数
C++的unordered_map函数是一个哈希表容器,用于存储键值对。它提供了快速的插入、查找和删除操作。与std::unordered_map相比,C++的unordered_map函数在性能上有一定的优势。根据概念证明,这种映射可以比std::unordered_map快3倍。因此,如果需要一个高性能的哈希表容器,可以考虑使用C++的unordered_map函数。
unordered_map函数的API与std::unordered_map类似。它包含了一些常用的成员函数,如at、insert、erase、find等。通过at函数,可以根据键值获取相应的值。unordered_map也支持自定义类型作为键和值,但需要自己定义哈希函数和比较函数。对于基本类型(如int、char、long等)和string类型,STL提供了默认的哈希函数和比较函数。而对于自定义类型,需要自己定义相应的哈希函数和比较函数。
总之,C++的unordered_map函数是一个高性能的哈希表容器,提供了类似std::unordered_map的API。它适用于存储键值对,并支持自定义类型作为键和值。如果需要高效地进行插入、查找和删除操作,可以考虑使用C++的unordered_map函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [C++ unordered_map](https://blog.csdn.net/m0_67393619/article/details/124503669)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [open_addressing_hash_table:在 C++ 上打开寻址哈希表](https://download.csdn.net/download/weixin_42117224/19926502)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
c++ unordered_map 常用函数
unordered_map 是 C++ STL 中的一个关联容器,用于存储键值对,其内部实现是基于哈希表。
下面是 unordered_map 的一些常用函数:
1. `insert`:插入键值对或列表。
```cpp
unordered_map<int, string> myMap;
myMap.insert({1, "apple"});
myMap.insert(make_pair(2, "banana"));
```
2. `erase`:删除指定键的键值对。
```cpp
myMap.erase(1);
```
3. `find`:查找指定键对应的迭代器,若不存在则返回 end()。
```cpp
auto itr = myMap.find(2);
if (itr != myMap.end()) {
// found
cout << itr->second << endl; // 输出 banana
}
```
4. `at`:访问指定键对应的值,若不存在则抛出异常。
```cpp
cout << myMap.at(2) << endl; // 输出 banana
```
5. `size`:返回键值对的数量。
```cpp
cout << myMap.size() << endl;
```
6. `empty`:判断容器是否为空。
```cpp
if (myMap.empty()) {
// container is empty
}
```
7. `clear`:清空容器中的所有键值对。
```cpp
myMap.clear();
```
这些是 unordered_map 的一些常用函数,还有其他函数可供使用,具体可以参考 C++ STL 的文档。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)