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

相关文章

gradle第三方Jar包依赖统一管理方式

《gradle第三方Jar包依赖统一管理方式》:本文主要介绍gradle第三方Jar包依赖统一管理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景实现1.顶层模块build.gradle添加依赖管理插件2.顶层模块build.gradle添加所有管理依赖包

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Linux之systemV共享内存方式

《Linux之systemV共享内存方式》:本文主要介绍Linux之systemV共享内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、工作原理二、系统调用接口1、申请共享内存(一)key的获取(二)共享内存的申请2、将共享内存段连接到进程地址空间3、将

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

JAVA保证HashMap线程安全的几种方式

《JAVA保证HashMap线程安全的几种方式》HashMap是线程不安全的,这意味着如果多个线程并发地访问和修改同一个HashMap实例,可能会导致数据不一致和其他线程安全问题,本文主要介绍了JAV... 目录1. 使用 Collections.synchronizedMap2. 使用 Concurren

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代