将单独文本文件的段拆分为单独的字符串

时间:2014-10-05 08:40:12

标签: c++ arrays string sorting visual-c++

关于将段落从单独的文本文件拆分为自己的字符串,我想提供一些建议/帮助。到目前为止,我所拥有的代码只计算该段落中的单词总数,但我想将其拆分,以便每行为1个句子,然后计算该句子/行中有多少单词然后将其放入其中。自己的阵列,所以我可以用特定的感知/线做其他事情。这是我的代码明智:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{
 std::ifstream inFile;
 inFile.open("Rhymes.txt", std::ios::in);
 if (inFile.is_open())
 {
     string word;
     unsigned long wordCount = 0;

     while (!inFile.eo())
     {
        inFile >> word;
        if (word.length() > 0)
        {
            wordCount++;
        }
     }

     cout << "The file had " << wordCount << " word(s) in it." << endl;
 } 


 system("PAUSE");
 return 0;
}

单独的文本文件名为&#34; Rhymes.txt&#34;那包含:

Today you are You, that is truer than true. There is no one alive who is Youer than You.
The more that you read, the more things you will know. The more that you learn, the more places you'll go.
How did it get so late so soon? Its night before its afternoon.
Today was good. Today was fun. Tomorrow is another one.
And will you succeed? Yes indeed, yes indeed! Ninety-eight and three-quarters percent guaranteed!
Think left and think right and think low and think high. Oh, the things you can think up if only you try!
Unless someone like you cares a whole awful lot, nothing is going to get better. It's not.
I'm sorry to say so but, sadly it's true that bang-ups and hang-ups can happen to you.

所以第一行是它自己的句子,当代码执行时,它会说:

The line has 19 words in it

我有点困惑,我也会这样做。我已经看过将句子分成单词的例子,但我找不到任何我真正能理解的与我要求的内容有关的内容。

2 个答案:

答案 0 :(得分:0)

假设每个空格只是一个空白字符,并且没有plenking / klemping,您可以通过std::count进行计数。阅读这些内容可以通过std::getline完成。

int main()
{
    // Simulating the file:
    std::istringstream inFile(
R"(Today you are You, that is truer than true. There is no one alive who is Youer than You.
The more that you read, the more things you will know. The more that you learn, the more places you'll go.
How did it get so late so soon? Its night before its afternoon.
Today was good. Today was fun. Tomorrow is another one.
And will you succeed? Yes indeed, yes indeed! Ninety-eight and three-quarters percent guaranteed!
Think left and think right and think low and think high. Oh, the things you can think up if only you try!
Unless someone like you cares a whole awful lot, nothing is going to get better. It's not.
I'm sorry to say so but, sadly it's true that bang-ups and hang-ups can happen to you.)");

    std::vector<std::string> lines; // This vector will contain all lines.

    for (std::string str; std::getline(inFile, str, '\n');)
    {
        std::cout << "The line has "<< std::count(str.begin(), str.end(), ' ')+1 <<" words in it\n";
        lines.push_back(std::move(str)); // Avoid the copy.
    }

    for (auto const& s : lines)
        std::cout << s << '\n';
}

如果您稍后需要每个句子中的单词数量,请保存std::pair<std::string, std::size_t>以保存行和单词计数 - 将循环体更改为:

        std::size_t count = std::count(str.begin(), str.end(), ' ') + 1;
        std::cout << "The line has "<<count<<" words in it\n";
        lines.emplace_back(std::move(str), count);

答案 1 :(得分:0)

我写了类似的东西:

vector<string> read_line()
{  string line, w;
   vector<string> words;

   getline(cin, line);
   stringstream ss(line);

   while(ss >> w)
     words.push_back(w);

   return words;
}

返回的向量包含您需要的信息:单词数量和单词本身(可以轻松删除标点符号)。

vector<string> words = read_line();
cout << "This line has " << words.size() << " words in it" << endl;

要阅读您所做的所有行:

while(1)
{  vector<string> words = read_line();
   if(words.size() == 0) break;

   // process line
}