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