将一段文本加载到字符串向量中



按原样运行该程序时,返回的文本不带任何空格。我如何让它分别识别每个单独的单词?

  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);
            }
    }

}
当前输出:

Thisissomesimplebasetexttouseforcomparisonwithotherfiles.Youmayuseyourownifyousochoose;yourprogramshouldn'tactuallycare.Forgettinginterestingresults,longerpassagesoftextmaybeuseful.Intheory,afullnovelmightwork,althoughitwilllikelybesomewhatslow.

期望的输出应该在每个单词之间有空格。在此之后,我需要能够按字母顺序对单词进行排序,因此我需要修复的不仅仅是输出。

修改

cout<<words[i];

cout << words[i] << ' ';

记住你的程序完全按照你的指令去做。如果你不让它输出空格,它就不会。

当您输出带有cout

的单词时需要添加一个包含空格的字符串
cout << " " << words[i];

最新更新