+ 2

What is the problem in erase function ?

the assignment is : "Use the map<string, int> STL container to count occurrences of words. Use the word as the key and the count as the value. - User should be able to enter words as desired until the word “done” is written. - After entering all words, your program should print each word with its count. - Then, erase all words that start with letter ‘a’ and print again." https://code.sololearn.com/crMHfR8jH9il/?ref=app

30th Mar 2019, 12:20 PM
Mona Mohamed
Mona Mohamed - avatar
7 Answers
+ 2
Erasing invalidates the iterator(s). erase returns a new and valid iterator to the next object. This also means that you shouldn't advance the iterator when you erase or risk going over the end.
30th Mar 2019, 2:18 PM
Dennis
Dennis - avatar
+ 1
when i run it, it give me run time error!
30th Mar 2019, 12:21 PM
Mona Mohamed
Mona Mohamed - avatar
0
i do not run the code on sololearn, i run it on codeblocks
30th Mar 2019, 2:27 PM
Mona Mohamed
Mona Mohamed - avatar
0
i tried to write : Map_Words.erase(it++); and it erase correctly but not at the same time, it need to call the function more times, but i need to erase all words from one call to the function.
30th Mar 2019, 6:31 PM
Mona Mohamed
Mona Mohamed - avatar