从std :: set C++中删除对重复项

Adr*_*ian 1 c++ stl

您好我有一个std ::对的包含以下元素:

set<pair<string, int> > mapElem;

apples 1
apples 2
at 1
eating 1
eating 2
football 1
going 1
today 1
today 2
today 3
today 4
today 5
tommy 1
tommy 2
Run Code Online (Sandbox Code Playgroud)

我需要找到一种方法来删除重复项并保留仅具有最高第二对的方法.(对不起,如果这个或标题令人困惑)编辑:如果可能,不使用std :: map!

apples 2
at 1
eating 2
football 1
going 1
today 5
tommy 2
Run Code Online (Sandbox Code Playgroud)

Eri*_*rik 5

map<string, int> aMap(mapElem.begin(), mapElem.end());
set<pair<string, int> > Temp(aMap.begin(), aMap.end());
mapElem.swap(Temp);
Run Code Online (Sandbox Code Playgroud)

没有地图:

set<pair<string, int> >::iterator nextit = mapElem.begin();
while (nextit != mapElem.end()) {
  set<pair<string, int> >::iterator it = nextit++;
  if (nextit != mapElem.end()) {
    if (it->first == nextit->first) {
      mapElem.erase(it);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)