久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

How do I iterate over cin line by line in C++?(如何在 C++ 中逐行迭代 cin?)
本文介紹了如何在 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 久久精品屋 | 三级视频在线观看电影 | av喷水| 成人免费小视频 | 亚洲入口| 亚洲一区欧美一区 | 91免费电影| 在线观看黄色电影 | 久久国内 | 国产成人免费视频网站视频社区 | 国产ts人妖系列高潮 | 欧美小视频在线观看 | 九热在线 | 欧美一区二区大片 | 在线视频中文字幕 | 婷婷色成人 | 午夜视频在线观看网站 | 午夜av毛片 | 亚洲成人蜜桃 | 中文字幕在线精品 | 中文字幕在线观看av | 成人免费视频网站在线观看 | 一区二区中文 | 99热在线免费 | 91爱爱·com | 中文字幕日韩欧美 | 日韩精品在线播放 | a级毛片免费高清视频 | 亚洲美女一区二区三区 | 欧美a在线| 国产精品一区二区三区在线播放 | 亚洲精品av在线 | 亚洲一区二区视频 | 中文字幕日韩一区 | 欧美乱码精品一区二区三区 | 日韩网站在线观看 | 欧美日韩一区二区三区不卡视频 | 国产黄色在线观看 | 91亚洲精选 | 欧美专区在线 | 欧美日韩亚洲在线 |