How to do "getline" from a std::string?

user1873947 picture user1873947 · Dec 12, 2012 · Viewed 27.6k times · Source

I have a problem. I load the whole file and then getline through it to get some info. However in the map format there may be 0 or 20 "lines" with the info. I need to know how to getline through std::string. There is a function (source stream, destination string, decimal) but I need (source string, destination string, decimal). Searching in streams isn't possible in C++ (only using many temp string and extracting and inserting many times, it's unclear and I don't want to do it that messy way). So I want to know how to getline from a std::string.

Thans

Answer

Some programmer dude picture Some programmer dude · Dec 12, 2012

You seem to want std::istringstream, which is in the header <sstream>:

std::string some_string = "...";

std::istringstream iss(some_string);

std::string line;
while (std::getline(iss, line))
{
    // Do something with `line`
}