开发者

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 implem开发者_JS百科ent 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!
}


Um, you can write a custom iterator for std::string that would iterate over string segments separated by newlines and pass a std::pair of such iterators to BOOST_FOREACH


You can use boost::tokenizer with \n token to iterate over lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜