我正在嘗試創建一個將文件讀入字符串向量並計算每個唯一單詞出現次數的字典。這是我到目前爲止:計算字符串向量中單詞的出現次數
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