我正在按照一个示例来计算一个单词在给定输入中出现的次数。这是我的代码:
string word, the_word;
int count(0);
vector<string> sentence;
auto it = sentence.begin();
cout << "Enter some words. Ctrl+z to end." << endl;
while (cin >> word)
sentence.push_back(word);
the_word = *sentence.begin();
cout << the_word << endl;
while(it != sentence.end()) {
if(*sentence.begin() == the_word)
++count;
++it;
}
cout << count << endl;
我给出的输入是“现在怎么样现在棕色奶牛”。我希望count
是 3,但我得到的是 200 万的整数。我错过了什么?