该代码完美运行,但我对它的工作原理感到困惑。我完全不明白为什么我们需要对字符串向量进行排序。它只会按字母顺序对单词进行排序,对吗?此外,当我们将它与字符串变量“Previous”进行比较时,它如何检测任何单词而不仅仅是相邻的单词
#include <iostream>
#include <vector>
using namespace std;
void detect(vector<string> vs);
int main() {
vector<string> vs;
string current;
while (cin>>current)
vs.push_back(current);
sort(vs.begin(), vs.end());
detect (vs);
system("pause");
}
void detect(vector<string> vs){
string previous = " ";
int index = 0;
while (index < vs.size()) {
if (vs[index]==previous) {
cout<<"repeated words: " <<previous<< endl;
}
previous = vs[index];
index++;
}
}