求助
查看原帖
求助
722468
MrJC_Pandingding楼主2023/3/12 08:25
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp={{1,2},{2,3},{4,5}};
map<int,int>::iterator it;
void eras(map<int,int>mp,int x)
{
	map<int,int>::iterator it;
	for(it=mp.begin();it!=mp.end();++it)
	{
		if(it->first==x)
		{
			mp.erase(it);
			return;
		}
	}
	return;
}
int main()
{
	for(it=mp.begin();it!=mp.end();++it)
		printf("%d %d\n",it->first,it->second);
	puts("\n---------------------------------------------------\n");
	eras(mp,2);
	for(it=mp.begin();it!=mp.end();++it)
		printf("%d %d\n",it->first,it->second);
	return 0;
}

eras()eras()函数代表将mapmap中键(firstfirst)为xx的元素删除,一运行发现没删掉。

输出结果:

1 2
2 3
4 5

---------------------------------------------------

1 2
2 3
4 5

哪位大佬帮帮我,悬赏关注(我知道系统提供了根据键删除的函数,但是当时是在比赛,我不知道。能不能不用系统自带的根据键删除的函数把mapmap中相应的元素删除)

2023/3/12 08:25
加载中...