我试图将一段文字读入一个字符串向量,然后创建字典,记录每个字的出现次数。到目前为止,它只加载文本的第一个单词,我不知道如何继续。我知道我不清楚如何正确使用这些成员函数。将一段文字读入一个字符串向量
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