問題描述
我想逐行迭代 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模板網!