我試圖將一段文字讀入一個字符串向量,然後創建字典,記錄每個字的出現次數。到目前爲止,它只加載文本的第一個單詞,我不知道如何繼續。我知道我不清楚如何正確使用這些成員函數。將一段文字讀入一個字符串向量
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 line;
while (file>>line)
{
words.push_back(line);
}
cout<<words[0];
}
預期輸出:
This is some simple base text to use for comparison with other files.
You may use your own if you so choose; your program shouldn't actually care.
For getting interesting results, longer passages of text may be useful.
In theory, a full novel might work, although it will likely be somewhat slow.
實際輸出:
This
任何想法,我將如何進行跟蹤出現的每個字的數量? – iamthewalrus 2013-04-26 19:46:14
@AndyMiller,地圖,也許? – chris 2013-04-26 19:46:51
@WhozCraig提出了一個挑戰。要按頻率排序: – qPCR4vir 2013-04-27 21:01:22