我正在尝试创建一个将文件读入字符串向量并计算每个唯一单词出现次数的字典。这是我到目前为止:计算字符串向量中单词的出现次数
int main()
{
ifstream input1;
input1.open("Base_text.txt");
vector<string> base_file;
vector<int> base_count;
if (input1.fail())
{
cout<<"Input file 1 opening failed."<<endl;
exit(1);
}
make_dictionary(input1, base_file, base_count);
}
void make_dictionary(istream& file, vector<string>& words, vector<int>& count)
{
string word;
int i=0;
while (file>>word)
{
words.push_back(word);
cout<<words[i];
i++;
}
for (i=0; i<words.size(); i++)
{
if ((words[i+1]!=words[i]))
{
count.push_back(i);
}
}
问题1:如何获得包含空格和识别单个词的向量? 问题2:任何想法如何继续第二部分(for循环)?
你可以使用提升? – 2013-04-26 21:47:17
[计数每个单词在文件中出现的次数]可能的重复(http://stackoverflow.com/questions/6103927/count-the-number-of-times-each-word-occurs-in-a-文件) – 2013-04-26 22:05:16