【『茴』的四种写法】 C++11 map和unordered_map遍历方法

本文详细介绍了C++11中遍历std::map和std::unordered_map的四种方法,包括使用迭代器iterator,类型萃取value_type,实际类型pair以及结构化绑定auto。通过示例代码展示每种方法的具体应用,并给出了遍历结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

map和unordered_map的遍历方法是相同的,不过遍历结果,map是有序的,unoredred_map遍历是无序的。 std:map 是个有序的关系容器,其完整原型如下:
template<
	class Key,
	class T,
	class Compare=std::less<Key>,
	class Allocator=std::allocator<std::pair<const Key,T>>
>class map;

键值对会根据键来用 Compare 排序,具体到 std::map<int, int> 就是:

map<int,int,std::less<int>>

一、迭代器 iterator


for(map<int,int>::iterator it=mp.begin();it!=mp.end();++it)
	cout<<it->first<<"--"<<it->second<<"\t";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值