int main()
{
map<string, size_t> word_count;
set<string> exclude = {"the", "but", "and", "or", "an", "a"};
string word;
while (cin >> word) {
for (auto beg = word.begin(); beg != word.end(); ++beg) {
if (isalpha(*beg) && isupper(*beg)) {
*beg = tolower(*beg);
}
if (ispunct(*beg)) {
beg = word.erase(beg);
}
}
if (exclude.find(word) == exclude.end())
++word_count[word];
}
for (constauto &w : word_count) {
cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times" : " time") << endl;
}
return 0;
}
Edit:
Went over what the for loop does and found the problem. After I initialise it from erase, it points at the last element and then the for loop increments that.