自从 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));
关键的一点是,当你reading某些东西,你指定一行——但除此之外,你只有字符串。
另一种可能性是使用大多数人几乎不知道其存在的标准库的一部分,更不用说有很多实际用途了。当您使用运算符>>读取字符串时,流返回一个字符串,直到该流的区域设置所说的空白字符为止。特别是如果您正在做大量面向行的工作,那么使用仅将换行符分类为空白的 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];
}
};
要使用此功能,您可以使用该方面将区域设置注入要从中读取的流,然后正常读取字符串,并且字符串的运算符>>始终读取整行。例如,如果我们想读取行,并按排序顺序写出唯一的行,我们可以使用如下代码:
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
通常会失败)。