如何在C ++中逐行迭代cin? [英] How do I iterate over cin line by line in C++?

查看:120
本文介绍了如何在C ++中逐行迭代cin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐行迭代 std :: cin ,将每一行作为 std :: string 。哪个更好:

I want to iterate over std::cin, line by line, addressing each line as a std::string. Which is better:

string line;
while (getline(cin, line))
{
    // process line
}

for (string line; getline(cin, line); )
{
    // process line
}

推荐答案

由于UncleBen提出了LineInputIterator,所以我想添加几个替代方法。首先,一个非常简单的类作为一个字符串代理:

Since UncleBen brought up his LineInputIterator, I thought I'd add a couple more alternative methods. First up, a really simple class that acts as a string proxy:

class line {
    std::string data;
public:
    friend std::istream &operator>>(std::istream &is, line &l) {
        std::getline(is, l.data);
        return is;
    }
    operator std::string() const { return data; }    
};

这样,你仍然会使用正常的istream_iterator读取。例如,要将文件中的所有行读取到字符串向量中,可以使用以下形式:

With this, you'd still read using a normal istream_iterator. For example, to read all the lines in a file into a vector of strings, you could use something like:

std::vector<std::string> lines;

std::copy(std::istream_iterator<line>(std::cin), 
          std::istream_iterator<line>(),
          std::back_inserter(lines));

关键的一点是,当你阅读

另一种可能性是使用标准库的一部分,大多数人几乎不知道存在,更不用说真的使用。当使用operator >>读取字符串时,流返回一个字符串,直到流的语言环境说的是一个空格字符。特别是如果你做很多工作都是面向行的,可以方便地创建一个具有ctype facet的区域设置,只将新行分类为白色空间:

Another possibility uses a part of the standard library most people barely even know exists, not to mention being of much real use. When you read a string using operator>>, the stream returns a string of characters up to whatever that stream's locale says is a white space character. Especially if you're doing a lot of work that's all line-oriented, it can be convenient to create a locale with a ctype facet that only classifies new-line as white-space:

struct line_reader: std::ctype<char> {
    line_reader(): std::ctype<char>(get_table()) {}
    static std::ctype_base::mask const* get_table() {
        static std::vector<std::ctype_base::mask> 
            rc(table_size, std::ctype_base::mask());

        rc['\n'] = std::ctype_base::space;
        return &rc[0];
    }
};  

要使用此功能,您将使用该方面,那么正常读取字符串,并且对于字符串的运算符>>总是读取整行。例如,如果我们想读入行,并按排序顺序写出唯一的行,我们可以使用如下代码:

To use this, you imbue the stream you're going to read from with a locale using that facet, then just read strings normally, and operator>> for a string always reads a whole line. For example, if we wanted to read in lines, and write out unique lines in sorted order, we could use code like this:

int main() {
    std::set<std::string> lines;

    // Tell the stream to use our facet, so only '\n' is treated as a space.
    std::cin.imbue(std::locale(std::locale(), new line_reader()));

    std::copy(std::istream_iterator<std::string>(std::cin), 
        std::istream_iterator<std::string>(), 
        std::inserter(lines, lines.end()));

    std::copy(lines.begin(), lines.end(), 
        std::ostream_iterator<std::string>(std::cout, "\n"));
    return 0;
}

请记住,这会影响流中的所有输入。使用这几乎排除了将面向行的输入与其他输入混合(例如,使用 stream>> my_integer 从流中读取数字通常会失败)。

Keep in mind that this affects all input from the stream. Using this pretty much rules out mixing line-oriented input with other input (e.g. reading a number from the stream using stream>>my_integer would normally fail).

这篇关于如何在C ++中逐行迭代cin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆