C++ std::map几种遍历方式(正序、倒序)

2024-02-05 03:36

本文主要是介绍C++ std::map几种遍历方式(正序、倒序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C++ std::map几种遍历方式(正序、倒序)

文章目录

  • C++ std::map几种遍历方式(正序、倒序)
    • 1、map 的定义方式
    • 2、正序遍历 map
      • 2.1 使用 for 循环
      • 2.2 使用 while 循环
    • 3、倒序遍历 map
      • 3.1 使用 for 循环
      • 3.2 使用 while 循环
    • 4、使用 std::greater 属性,直接定义倒序存储的 map
      • 4.1 使用 for 循环
      • 4.2 使用 while 循环

1、map 的定义方式


//默认定义格式(默认按key升序存储): key, value,其中key可以是任意类型
std::map<std::uint32_t, std::string> myMap;  //key 值为 std::uint32_t 类型
std::map<std::string, std::string> myMap;    //key 值为 std::string 类型//指定数据按key升序存储
std::map<std::uint32_t, std::string, std::greater<std::uint32_t> > myMap; //指定数据按key升序存储
std::map<std::uint32_t, std::string, std::less<std::uint32_t> > myMap; 

2、正序遍历 map

注意:正序使用的是 std::map<std::uint32_t, std::string>::iterator, 倒序使用的是:std::map<std::uint32_t, std::string>::reverse_iterator。

2.1 使用 for 循环


#include <iostream>
#include <map>int main()
{std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};// 使用迭代器倒序遍历mapstd::map<std::uint32_t, std::string>::iterator iter;for (iter = myMap.begin(); iter != myMap.end(); ++iter) {std::cout << iter->first << " => " << iter->second << '\n';}return 0;
}

2.2 使用 while 循环


#include <iostream>
#include <map>int main()
{std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};std::map<std::uint32_t, std::string>::iterator iter = myMap.begin(); // 使用迭代器倒序遍历mapwhile (iter != myMap.end()) {std::cout << iter->first << " => " << iter->second << '\n';++it;}return 0;
}

3、倒序遍历 map

注意:正序使用的是 std::map<std::uint32_t, std::string>::iterator, 倒序使用的是:std::map<std::uint32_t, std::string>::reverse_iterator。

3.1 使用 for 循环


#include <iostream>
#include <map>int main()
{std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};// 使用迭代器倒序遍历mapstd::map<std::uint32_t, std::string>::reverse_iterator iter;for (iter = myMap.rbegin(); iter != myMap.rend(); ++iter) {std::cout << iter->first << " => " << iter->second << '\n';}return 0;
}

3.2 使用 while 循环


#include <iostream>
#include <map>int main()
{std::map<std::uint32_t, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};std::map<std::uint32_t, std::string>::reverse_iterator iter = myMap.rbegin(); // 使用迭代器倒序遍历mapwhile (iter != myMap.rend()) {std::cout << iter->first << " => " << iter->second << '\n';++it;}return 0;
}

4、使用 std::greater 属性,直接定义倒序存储的 map

注意:正序使用的是 std::map<std::uint32_t, std::string>::iterator, 倒序使用的是:std::map<std::uint32_t, std::string>::reverse_iterator。

4.1 使用 for 循环


#include <iostream>
#include <map>int main()
{std::map<std::uint32_t, std::string, std::greater<std::uint32_t> > myMap = {{1, "one"}, {2, "two"}, {3, "three"}};// 使用迭代器倒序遍历mapstd::map<std::uint32_t, std::string>::iterator iter;for (iter = myMap.begin(); iter != myMap.end(); ++iter) {std::cout << iter->first << " => " << iter->second << '\n';}return 0;
}

4.2 使用 while 循环


#include <iostream>
#include <map>int main()
{std::map<std::uint32_t, std::string, std::greater<std::uint32_t> > myMap = {{1, "one"}, {2, "two"}, {3, "three"}};std::map<std::uint32_t, std::string>::iterator iter = myMap.begin(); // 使用迭代器倒序遍历mapwhile (iter != myMap.end()) {std::cout << iter->first << " => " << iter->second << '\n';++it;}return 0;
}

这篇关于C++ std::map几种遍历方式(正序、倒序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679645

相关文章

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

C++包装器

包装器 在 C++ 中,“包装器”通常指的是一种设计模式或编程技巧,用于封装其他代码或对象,使其更易于使用、管理或扩展。包装器的概念在编程中非常普遍,可以用于函数、类、库等多个方面。下面是几个常见的 “包装器” 类型: 1. 函数包装器 函数包装器用于封装一个或多个函数,使其接口更统一或更便于调用。例如,std::function 是一个通用的函数包装器,它可以存储任意可调用对象(函数、函数

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

06 C++Lambda表达式

lambda表达式的定义 没有显式模版形参的lambda表达式 [捕获] 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 有显式模版形参的lambda表达式 [捕获] <模版形参> 模版约束 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 含义 捕获:包含零个或者多个捕获符的逗号分隔列表 模板形参:用于泛型lambda提供个模板形参的名

内核启动时减少log的方式

内核引导选项 内核引导选项大体上可以分为两类:一类与设备无关、另一类与设备有关。与设备有关的引导选项多如牛毛,需要你自己阅读内核中的相应驱动程序源码以获取其能够接受的引导选项。比如,如果你想知道可以向 AHA1542 SCSI 驱动程序传递哪些引导选项,那么就查看 drivers/scsi/aha1542.c 文件,一般在前面 100 行注释里就可以找到所接受的引导选项说明。大多数选项是通过"_

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

webm怎么转换成mp4?这几种方法超多人在用!

webm怎么转换成mp4?WebM作为一种新兴的视频编码格式,近年来逐渐进入大众视野,其背后承载着诸多优势,但同时也伴随着不容忽视的局限性,首要挑战在于其兼容性边界,尽管WebM已广泛适应于众多网站与软件平台,但在特定应用环境或老旧设备上,其兼容难题依旧凸显,为用户体验带来不便,再者,WebM格式的非普适性也体现在编辑流程上,由于它并非行业内的通用标准,编辑过程中可能会遭遇格式不兼容的障碍,导致操

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M