如何在 C++ 中逐行迭代 cin?

时间:2022-11-19
本文介绍了如何在 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
}

?执行此操作的常规方法是什么?

? What is the normal way to do this?

推荐答案

自从 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));

关键在于,当你阅读某物时,你指定了一行——否则,你就只有字符串.

The crucial point is that when you're reading something, you specify a line -- but otherwise, you just have strings.

另一种可能性使用标准库的一部分,大多数人甚至几乎不知道存在,更不用说实际用途了.当您使用 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['
'] = std::ctype_base::space;
        return &rc[0];
    }
};  

要使用它,您需要使用使用该方面的语言环境为要从中读取的流注入数据,然后正常读取字符串,而对于字符串的 operator>> 始终读取整行.例如,如果我们想按行读取,并按排序顺序写出唯一的行,我们可以使用这样的代码:

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 '
' 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, "
"));
    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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何使用 QueryPerformanceCounter? 下一篇:当模板参数用作另一个模板的模板参数时,为什

相关文章

最新文章