Некоторые ошибки:
if (sentence[i] == sentence[i+1]) {
Ваш цикл позволяет мне быть size()-1
- Таким образом, вы читаете один за концом векторного предложения.
while (i != sentence.size()) {
while (j != sentence.size()) {
...
++j;
}
++i;
}
j никогда не сбрасывается - следующая итерация внешнего цикла будет начинаться с sentence.size()
- вы, вероятно, не хотите этого.
Вы должны решить это с помощью std::map<std::string, int>
:
std::map<std::string, int> words;
while (cin >> word) {
if (word == "ctrlz") {
break;
}
words[word] += 1;
}
for (std::map<std::string>::const_iterator it = words.begin(); it != words.end(); ++it) {
if (it->second > 1) {
cout << it->second << " copies of " << it->first << endl;
}
}