删除map中元素再返回it->second导致1WA
#include <cstdio>
#include <map>
using namespace std;
int op, K, P;
map<int, int> mp;
int gethigh() {
if (!mp.size())
return 0;
map<int, int>::iterator it=mp.end();
it--;
int res=it->second;
mp.erase(it->first);
return res;
}
int getlow() {
if (!mp.size())
return 0;
map<int, int>::iterator it=mp.begin();
int res=it->second;
mp.erase(it->first);
return res;
}
int main()
{
//freopen("in.txt", "r", stdin);
while (~scanf("%d", &op)&&op!=0) {
if (op==2) {
printf("%d\n", gethigh());
} else if (op==3) {
printf("%d\n", getlow());
} else {
scanf("%d%d", &K, &P);
mp[P]=K;
}
}
return 0;
}
故用res先存,删除后返回res