开发者

Iterating over lines in a string?

开发者 https://www.devze.com 2023-01-25 23:48 出处:网络
I am trying to do something like this: BOOST_FOREACH (const std::string& line, allLinesOf(someFileLoadedIntoString))

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

精彩评论

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