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;
}
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – std::map正向遍历与反向遍历的几种方式
原文链接:https://www.stubbornhuang.com/1357/
发布于:2021年05月21日 14:11:10
修改于:2023年06月26日 21:35:03
当前分类随机文章推荐
- C++ - 阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点 阅读1619次,点赞0次
- C++ - 在两个互有依赖关系的类中使用std::shared_ptr和std::weak_ptr进行内存管理 阅读992次,点赞0次
- C++11/std::thread - 线程的基本用法 阅读3660次,点赞0次
- C++ - std::string字符串格式化方法总结 阅读2011次,点赞0次
- C++ - 根据给定分隔符分割字符串 阅读265次,点赞0次
- C++ - 使用正则判断字符串是否全是中文 阅读1688次,点赞0次
- C++ - 使用模板和智能指针构建一个双向链表工具类 阅读1293次,点赞0次
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读5899次,点赞2次
- C++ – Unicode编码下的全角字符转半角字符 阅读2740次,点赞0次
- C++ - Windows系统使用C++切换音频默认输出设备 阅读86次,点赞0次
全站随机文章推荐
- 深度学习 - 语音识别框架wenet的非流式与流式混合训练机制 阅读1906次,点赞0次
- opencv-python - 读取视频,不改变视频分辨率修改视频帧率 阅读5440次,点赞2次
- Pytorch – 使用torch.matmul()替换torch.einsum(‘nkctv,kvw->nctw’,(a,b))算子模式 阅读1390次,点赞0次
- Modern OpenGL - GLSL着色语言2:GLSL入口函数和GLSL中的变量 阅读3039次,点赞0次
- 资源分享 - Foundations of Game Engine Development, Volume 2 Rendering 英文高清PDF下载 阅读4164次,点赞2次
- C++ - 将std::vector中的数值拷贝到数组中 阅读3635次,点赞1次
- 深度学习 - 归纳轻量级神经网络(长期更新) 阅读431次,点赞0次
- Python - 使用onnxruntime加载和推理onnx模型 阅读795次,点赞0次
- C++ - 数组初始化 阅读573次,点赞0次
- 资源分享 - 精通Python网络爬虫 核心技术、框架与项目实战 ,韦玮著 高清PDF下载 阅读2222次,点赞0次
评论
169