#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()函数代表将map中键(first)为x的元素删除,一运行发现没删掉。
输出结果:
1 2
2 3
4 5
---------------------------------------------------
1 2
2 3
4 5
哪位大佬帮帮我,悬赏关注(我知道系统提供了根据键删除的函数,但是当时是在比赛,我不知道。能不能不用系统自带的根据键删除的函数把map中相应的元素删除)