I've got a std::multimap
that has multiple entries with the same key and value and I want to remove all duplicate values, only keeping the one with the highest index.
So far I've got something like this:
#include <iostream>
#include<iterator>
#include <map>
int main(void)
{
std::multimap<double, std::string> m;
m.insert(std::pair<double,std::string>(79.43567,"ARH1265"));
m.insert(std::pair<double,std::string>(82.96567,"ARH1265"));
m.insert(std::pair<double,std::string>(94.03261,"TE5748"));
m.insert(std::pair<double,std::string>(73.53961,"TE5748"));
m.insert(std::pair<double,std::string>(93.43567,"CRP5285"));
std::cout << "size: " << m.size() << std::endl;
std::multimap<double, std::string>::iterator it;
for (it = m.begin(); i!=m.end(); i++) {
// ???
}
}
and I'll want to end up with a map with three entries in the map:
82.96567, ARH1265
94.03261, TE5748
93.43567, CRP5285
If you can, replace the std::multimap
by a std::map
or a std::unordered_map
, and reverse the key and values. (key = string
and value = double
).
When inserting, check if the value you want to insert is bigger than the value in the map, if not, don't insert.
If you can't replace the multimap, you can use the approach above with a temporary map, clear the multimap and insert the contents of the temporary map back into the multimap.