如果遍历map中最后一个元素rbegin(),end(),rend()

#include "stdafx.h"


#include <iostream>
#include <map>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	map<char,int> mymap;

	mymap['b'] = 100;
	mymap['a'] = 200;
	mymap['c'] = 300;
	map<char,int>::iterator it=mymap.begin();
	// show content:
	for (it; it!=mymap.end(); ++it)
		std::cout << it->first << " => " << it->second << '\n';
	//正确做法
	 //输出map中的最后一个元素
	map<char,int>::reverse_iterator iter = mymap.rbegin(); //返回最后一个元素
	/*mymap.end();
	mymap.rend();
	*/
	if (iter != mymap.rend())
	{
		std::cout << iter->first << " => " << iter->second << '\n';
	}

	//错误做法
	 map<char,int>::iterator iter2 = mymap.end(); //
	 //如果这样直接用会导致崩溃 
	 std::cout << iter2->first << " => " << iter2->second << '\n';
	if (iter2 != mymap.end())
	{
		std::cout << iter2->first << " => " << iter2->second << '\n';
	}

	/*mymap.end();
	mymap.rend();
	这两个函数只是标记找没找到 不是返回最后一个元素
	输出最后一个元素一定要切记
	Return iterator to end
	Returns an iterator referring to the past-the-end element in the map container.

	The past-the-end element is the theoretical element that would follow the last element in the map container. 
        It does not point to any element, and thus shall not be dereferenced.

	Because the ranges used by functions of the standard library do not include the element pointed by their closing iterator,
        this function is often used in combination with map::begin to specify a range including all the elements in the container.

	If the container is empty, this function returns the same as map::begin.
	*/
	return 0;
}

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++,有多种方式可以遍map。以下是三种常用的遍方式: 方式一:使用范围for循环 ```cpp for(auto &t : m){ cout<<"key:"<<t.first<<" value:"<<t.second<<endl; } ``` 这种方式使用了C++11引入的范围for循环,可以直接遍map的每个元素。 方式二:使用迭代器 ```cpp for(map<int,string>::iterator iter = m.begin(); iter != m.end(); ++iter){ cout<<"key:"<<iter->first<<" value:"<<iter->second<<endl; } ``` 这种方式使用了迭代器来遍map,通过`begin()`和`end()`函数获取map的起始和结束迭代器,然后使用循环遍每个元素。 方式三:使用反向迭代器 ```cpp for(map<int,string>::reverse_iterator iter = m.rbegin(); iter != m.rend(); ++iter){ cout<<"key:"<<iter->first<<" value:"<<iter->second<<endl; } ``` 这种方式使用了反向迭代器来遍map,通过`rbegin()`和`rend()`函数获取map的反向起始和结束迭代器,然后使用循环遍每个元素。 以上是三种常用的遍map的方式,你可以根据自己的需求选择适合的方式进行遍。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [c++map的三种方式](https://blog.csdn.net/m0_67390969/article/details/126317941)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C++ map的几种方式](https://blog.csdn.net/VariatioZbw/article/details/124947520)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C++map的遍](https://blog.csdn.net/chengqiuming/article/details/89815814)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值