0

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

  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.

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

4

2 回答 2

0

改变这个

cout<<words[i];

对此

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

请记住,您的程序完全按照您的指示行事。如果你不告诉它输出空格,它就不会。

于 2013-04-27T20:05:55.993 回答
0

输出单词时需要添加一个包含空格的字符串cout

cout << " " << words[i];
于 2013-04-27T20:06:34.610 回答