This is a continuation from my previous question
Hall of Fame[
^]
By following the advice from
Richard MacCutchan[
^], I tried using vector in my implementation of Hall Of Fame which has name and score.
The name should not have duplicate and score should be sorted.
However here I'm stuck.
bool myfunction(std::pair<string, unsigned int> rhs, std::pair<string,unsigned int> lhs) {
return ( rhs.second > lhs.second );
}
std::vector < std::pair<string, unsigned int>> hall;
hall.push_back(std::make_pair("One", 50));
hall.push_back(std::make_pair("Two", 30));
hall.push_back(std::make_pair("Three", 10));
hall.push_back(std::make_pair("Two", 30));
hall.push_back(std::make_pair("Three", 20));
std::sort(hall.begin(), hall.end(), myfunction);
for ( auto c : hall ) {
cout << c.first << " " << c.second << endl;
}
for ( auto a : hall ) {
for ( auto b : hall ) {
if ( ( a.first == b.first ) && ( a.second != b.second ) ) {
if ( a.second > b.second ) {
hall.erase(b); }
}
}
}
I've copy the vector into a set<pair><string,unsigned>> and it solve problem with duplicated entry ("Two",30).
How can I remove the pair("Three",10) from the vector.
Whatever loop / iterator trick that I know do not work.
In one of tries versions (using iterator) I managed to erase the pair("Three",10) but it failed in the next step (using debugger) because (I think) the iterator pointing to invalid location in the next iteration or vector size changed.
I'm a newbie to programming
I really think I missed something simple here.
What should I do. Please teach me a bit.
What I have tried:
after sorting the vector
-I've tried erasing using iterator pointing b.
-I've tried using another loop (int i=0;i<hall.size();>-unsuccessful using std::find to search for pairs that have same string but different value (hence the nested a and b for loop).
-reading cplusplus.com about it