c++ map怎么遍历
时间: 2023-07-01 16:28:04 浏览: 181
C++中的map可以使用迭代器进行遍历,示例代码如下:
```cpp
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> myMap;
myMap[1] = "apple";
myMap[2] = "banana";
myMap[3] = "orange";
myMap[4] = "grape";
// 使用迭代器遍历map
for (auto it = myMap.begin(); it != myMap.end(); it++) {
cout << it->first << " : " << it->second << endl;
}
return 0;
}
```
输出结果为:
```
1 : apple
2 : banana
3 : orange
4 : grape
```
其中,`auto`是C++11中的关键字,可以自动推导变量类型。`myMap.begin()`返回map的第一个元素的迭代器,`myMap.end()`返回最后一个元素之后的迭代器,因此遍历时需要判断迭代器是否到达了map的末尾。`it->first`和`it->second`分别表示迭代器指向的元素的键和值。
相关问题
c++ map遍历
### 遍历 `std::map` 的方法
在 C++ 中,可以采用多种方式来遍历 `std::map` 容器。以下是两种主要的方式:正向遍历和反向遍历。
#### 正向遍历
通过迭代器实现的正向遍历能够按照键值顺序访问每一个元素。下面展示了利用增强型for循环以及传统while循环来进行操作的具体实例[^2]:
##### 使用增强型 for 循环
这种方式简洁明了,适合于不需要修改容器内数据的情况。
```cpp
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
// 假设已经填充了一些数据到myMap
for (const auto& pair : myMap) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << '\n';
}
}
```
##### 利用 while 循环配合迭代器
此法提供了更灵活的操作空间,允许开发者执行更为复杂的逻辑处理。
```cpp
#include <iostream>
#include <map>
int main() {
std::map<int, std::string>::iterator it = myMap.begin();
while(it != myMap.end()) {
std::cout << "Key: " << (*it).first << ", Value: " << (*it).second << "\n";
++it;
}
}
```
#### 反向遍历
当需要逆序访问 map 中的数据时,则可借助 reverse_iterator 来完成这一目标。同样存在基于 for 和 while 实现的不同版本:
##### 采用 for 进行反向遍历
这种方法简单直观,易于理解和维护。
```cpp
#include <iostream>
#include <map>
int main() {
for(auto rit=myMap.rbegin(); rit!=myMap.rend();++rit){
std::cout << "Key: " << rit->first << ", Value: " << rit->second << "\n";
}
}
```
##### 应用 while 结合 rbegin/rend 方法
给予程序员更大的自由度去控制流程走向。
```cpp
#include <iostream>
#include <map>
int main() {
std::map<int,std::string>::reverse_iterator rit=myMap.rbegin();
while(rit!=myMap.rend()){
std::cout << "Key: " << rit->first << ", Value: " << rit->second << "\n";
++rit;
}
}
```
以上就是关于如何在 C++ 中遍历 `std::map` 的介绍及其具体实践案例。
c++ map 遍历
在C++中,可以使用迭代器、range for循环和C++17的结构化绑定来遍历map。
一种遍历方式是使用迭代器。可以通过使用map的begin()和end()函数来获取map的起始和结束迭代器,然后使用一个循环来迭代访问每个元素。例如,可以使用以下代码进行遍历:
#include <map>
#include <iostream>
int main() {
std::map<int, std::string> mapStudent;
mapStudent = "student_one";
mapStudent = "student_two";
mapStudent = "student_three";
std::map<int, std::string>::iterator iter; // 声明迭代器
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
std::cout << iter->first << " " << iter->second << std::endl;
}
return 0;
}
另一种遍历方式是使用C++11及以上版本的range for循环。可以直接使用auto关键字来声明一个变量来表示每个键值对,并通过range for循环遍历map。以下是示例代码:
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> mp;
mp["张三"] = 20;
mp["李四"] = 18;
mp["王五"] = 30;
std::cout << "方式一、迭代器" << std::endl;
for (auto it = mp.begin(); it != mp.end(); it++) {
std::cout << it->first << " " << it->second << std::endl;
}
std::cout << "\n方法二、range for" << std::endl;
for (auto it : mp) {
std::cout << it.first << " " << it.second << std::endl;
}
std::cout << "\n方法三" << std::endl;
for (auto [key, val] : mp) {
std::cout << key << " " << val << std::endl;
}
return 0;
}
另外还可以使用简单的for循环和size()函数来遍历map。以下是示例代码:
#include <map>
#include <iostream>
int main() {
std::map<int, std::string> mapStudent;
mapStudent = "student_one";
mapStudent = "student_two";
mapStudent = "student_three";
int iSize = mapStudent.size();
for (int i = 1; i <= iSize; i++) {
std::cout << i << " " << mapStudent[i] << std::endl;
}
return 0;
}
以上是C++中遍历map的几种方式。可以根据实际情况选择合适的方式来遍历map。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [C++中map的遍历](https://blog.csdn.net/chengqiuming/article/details/89815814)[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_1"}}] [.reference_item style="max-width: 50%"]
- *2* [C++ map遍历的几种方式](https://blog.csdn.net/VariatioZbw/article/details/124947520)[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_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.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)