多个字符串不同单词总数统计(大小写不敏感)
#include<string>
#include<iostream>
#include<vector>
#include<cctype>
#include<algorithm>
using namespace std;
int main(void)
{
vector<string> sentences;
sentences.push_back(" I am a software engineer");
sentences.push_back(" I Am right");
vector<string> words;
int wordsCount = 0;
for(string str : sentences) {
std::string::size_type i = 0;
while( i != str.size()) {
if(' ' != str[i]) {
string word;
while(i !=str.size() && str[i]!=' '){
word += tolower(str[i]);
++i;
}
vector<string>::iterator iter = find(words.begin(), words.end(), word);
if(iter == words.end()){
++wordsCount;
words.push_back(word);
}
}
else ++i;
}
}
cout << wordsCount;
return 0;
}