C++ – std::map正向遍历与反向遍历的几种方式
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – std::map正向遍历与反向遍历的几种方式
原文链接:https://www.stubbornhuang.com/1357/
发布于:2021年05月21日 14:11:10
修改于:2021年05月21日 14:11:10

1 std::map正向遍历
1.1 for循环
#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<int, std::string> t_Map;
t_Map[0] = "A";
t_Map[1] = "B";
t_Map[2] = "C";
std::map<int, std::string>::iterator iter1;
for (iter1 = t_Map.begin();iter1 != t_Map.end();iter1++)
{
std::cout << iter1->first << " : " << iter1->second << std::endl;
}
getchar();
return 0;
}
1.2 while循环
#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<int, std::string> t_Map;
t_Map[0] = "A";
t_Map[1] = "B";
t_Map[2] = "C";
std::map<int, std::string>::iterator iter2 = t_Map.begin();
while (iter2 != t_Map.end())
{
std::cout << iter2->first << " : " << iter2->second << std::endl;
iter2++;
}
getchar();
return 0;
}
2 std::map反向遍历
2.1 for循环
#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<int, std::string> t_Map;
t_Map[0] = "A";
t_Map[1] = "B";
t_Map[2] = "C";
std::map<int, std::string>::reverse_iterator iter1;
for (iter1 = t_Map.rbegin();iter1 != t_Map.rend();iter1++)
{
std::cout << iter1->first << " : " << iter1->second << std::endl;
}
getchar();
return 0;
}
2.2 while循环
#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<int, std::string> t_Map;
t_Map[0] = "A";
t_Map[1] = "B";
t_Map[2] = "C";
std::map<int, std::string>::reverse_iterator iter2 = t_Map.rbegin();
while (iter2 != t_Map.rend())
{
std::cout << iter2->first << " : " << iter2->second << std::endl;
iter2++;
}
getchar();
return 0;
}
当前分类随机文章推荐
- C++11/std::condition_variable - 生产者消费者模型 阅读2621次,点赞0次
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读2461次,点赞0次
- C++11/std::thread - 线程的基本用法 阅读3084次,点赞0次
- C++ - 使用ffmpeg读取视频旋转角度并使用OpenCV根据旋转角度对视频进行旋转复原 阅读1524次,点赞0次
- C++ - 一文搞懂std::future、std::promise、std::packaged_task、std::async的使用和相互区别 阅读41次,点赞0次
- C++ - C++类的特殊成员函数,析构函数,拷贝构造函数,移动构造函数,赋值运算符,移动赋值运算符介绍和基础语法 阅读648次,点赞0次
- C++ - std::string替换字符串中所有指定的子字符串 阅读1427次,点赞1次
- C++ - 将std::vector中的数值拷贝到数组中 阅读1787次,点赞1次
- C++ - 我的代码风格备忘 阅读644次,点赞0次
- C++ - 拷贝构造函数与拷贝构造函数调用时机 阅读153次,点赞0次
全站随机文章推荐
- Alphapose - 在Alphapose中使用yolov3-tiny检测器大幅提升检测性能 阅读2471次,点赞0次
- 资源分享 - 统计学习方法(李航著) 高清PDF下载 阅读4337次,点赞3次
- 网站个性化 - 添加人形时钟 honehone_clock.js 阅读2823次,点赞0次
- 计算几何与计算机图形学必读书单整理,附下载链接! 阅读16030次,点赞18次
- C++ - 字节数组byte[]或者unsigned char[]与int的相互转换 阅读6128次,点赞1次
- Python - 使用Opencv-Python库获取本机摄像头视频并保存为视频文件 阅读2385次,点赞0次
- 资源分享 - Guide to Computational Geometry Processing Foundations, Algorithms, and Methods英文高清PDF下载 阅读1451次,点赞0次
- 资源分享 - Tricks of the 3D Game Programming Gurus - Advanced 3D Graphics and Rasterization 英文高清PDF下载 阅读1291次,点赞0次
- 资源分享 - 3D Game Engine Architecture - Engineering Real-Time Applications with Wild Magic 英文高清PDF下载 阅读1306次,点赞0次
- OpenCV - 读取一个图像,并使用Canny算子进行边缘提取 阅读2869次,点赞0次
评论
164