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

30,395

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`
}
Share:
30,395

Related videos on Youtube

user1873947
Author by

user1873947

Updated on January 20, 2021

Comments

  • user1873947
    user1873947 almost 3 years

    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

  • user1873947
    user1873947 almost 11 years
    Thanks. Simple question related to it. How to make getline NOT to get a line when there is no decimal found in the stream?
  • user1873947
    user1873947 almost 11 years
    Because that's still a problem as I can't search in a stream (that's why I wanted string getline which doesn't exist).
  • Some programmer dude
    Some programmer dude almost 11 years
    @user1873947 The simplest is probably to check if the string you use contains a digit, and then skip the whole std::istringstream/std::getline bit.