简体   繁体   中英

Iterating over lines in a string?

I am trying to do something like this:

BOOST_FOREACH (const std::string& line, allLinesOf(someFileLoadedIntoString))
{
   ...
}

I wonder how to implement the allLinesOf function? Thanks!

UPDATE: Thanks for the answers. Sorry but I forgot to mention one important detail: in my case the newlines are \\r\\n.

You can use std::getline .

std::string line;
while(std::getline(file, line)) {
    // Ohai!
}

嗯,您可以为std :: string编写自定义迭代器,该迭代器将迭代由换行符分隔的字符串段,并将此类std :: pair传递给BOOST_FOREACH

您可以将boost :: tokenizer\\n令牌一起使用来遍历行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM