unordered_map 获取 keys
时间: 2023-10-15 09:23:25 浏览: 91
要获取 `unordered_map` 中的所有键(keys),可以使用一个循环遍历来实现。以下是一个示例代码:
```cpp
#include <iostream>
#include <unordered_map>
#include <vector>
int main() {
std::unordered_map<int, std::string> myMap;
myMap[1] = "apple";
myMap[2] = "banana";
myMap[3] = "orange";
// 获取所有的键
std::vector<int> keys;
for (const auto& pair : myMap) {
keys.push_back(pair.first);
}
// 打印所有的键
for (const auto& key : keys) {
std::cout << key << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果:
```
1 2 3
```
在这个例子中,我们首先创建了一个 `unordered_map`,并插入了一些键值对。然后,我们使用一个循环遍历 `myMap`,将每个键存储在 `keys` 向量中。最后,我们再次使用循环遍历 `keys` 向量,并打印出每个键。这样就可以获取到 `unordered_map` 中的所有键了。
相关问题
unordered_map keys
unordered_map中的keys是指存储在unordered_map中的所有键值对中的键。unordered_map是一个关联式容器,它允许通过keys快速地索引到与其对应的value。在unordered_map中,键值通常用于唯一地标识元素,而映射值是一个对象,其内容与此键关联。键和映射值的类型可以是不同的。unordered_map通过哈希函数将键映射到对应的桶中,相同哈希值的键值对会被放在同一个桶中。因此,通过keys可以快速地访问到对应的value。
#### 引用[.reference_title]
- *1* [【C++】STL —— unordered_map/unordered_set的基本使用](https://blog.csdn.net/sjsjnsjnn/article/details/128454738)[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^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [【C++从0到1】第十九篇:哈希(上)](https://blog.csdn.net/qq_52809807/article/details/124947410)[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^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *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^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
unordered_map
unordered_map is a container in C++ STL (Standard Template Library) that stores elements in a key-value pair format. It is implemented using a hash table, which makes it very efficient for lookup, insertion, and deletion operations. The keys in an unordered_map are unique, and the elements are sorted based on their hash values.
The syntax for declaring an unordered_map is as follows:
```c++
std::unordered_map <key_type, value_type> map_name;
```
Here, key_type is the data type of the key, and value_type is the data type of the value. For example, if we want to create an unordered_map of string keys and integer values, we can use the following syntax:
```c++
std::unordered_map <std::string, int> my_map;
```
We can insert elements into the unordered_map using the insert() function:
```c++
my_map.insert(std::make_pair("apple", 10));
```
We can access the value associated with a particular key using the [] operator:
```c++
int value = my_map["apple"];
```
We can also iterate over the elements of the unordered_map using iterators:
```c++
for(auto it = my_map.begin(); it != my_map.end(); ++it) {
std::cout << it->first << " : " << it->second << std::endl;
}
```
Overall, unordered_map provides a fast and efficient way of storing and accessing key-value pairs in C++.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)