自从 UncleBen 提出了他的 LineInputIterator,我想我应该再添加几个替代方法。首先,一个非常简单的类充当字符串代理:
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 进行阅读。例如,要将文件中的所有行读入字符串向量,您可以使用以下内容:
std::vector<std::string> lines;
std::copy(std::istream_iterator<line>(std::cin),
std::istream_iterator<line>(),
std::back_inserter(lines));
关键点是,当您阅读某些内容时,您指定了一行——否则,您只有字符串。
另一种可能性是使用大多数人几乎不知道存在的标准库的一部分,更不用说有很多实际用途了。当您使用 operator>> 读取字符串时,该流将返回一个字符串,直到该流的语言环境中所说的空白字符为止。尤其是如果您正在做大量面向行的工作,则可以方便地创建一个带有 ctype 方面的语言环境,该方面仅将换行符分类为空白:
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];
}
};
要使用它,您可以使用该构面为要读取的流注入语言环境,然后正常读取字符串,并且字符串的 operator>> 始终读取整行。例如,如果我们想读取行,并按排序顺序写出唯一的行,我们可以使用这样的代码:
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 从流中读取数字通常会失败)。