What is the best way to erase an element based on the code below? The internal for loop needs to finish before I can erase the biggest number found. While "problem-with-stdmapiterator-after-calling-erase" explains how erase works, I cannot seem to be able to make it work. An example based on the code below would be appreciated.
map<string,int> test;
void sort_print()
{
int biggestNum = 0;
string word;
for(int i = 0; i < WORDS_TO_FIND; i++)
{
for(auto it = test.begin(); it != test.end(); ++it )
{
if (it->second > biggestNum)
{
biggestNum = it->second;
word = it->first;
}
}
test.erase(word);
cout << word + ": " << biggestNum << endl;
}
}
Interesting enough, when I go to main (after having removed the first loop from the function above and do the following.. then it works but why?:
for(int i = 0; i < WORDS_TO_FIND; i++)
{
test.erase(sort_print());
}