Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 986
user1873947 Avatar asked Dec 12 '12 10:12

user1873947


People also ask

Can you Getline from a string?

To accept a string or a line of input stream as input, we have an in-built function called getline(). This function is under the <string> header file. It accepts all the strings until a newline character is encountered.

Does CIN Getline work with string?

In C++, the cin object also allows input from the user, but not multi-word or multi-line input. That's where the getline() function comes in handy. The function continues accepting inputs and appending them to the string until it encounters a delimiting character.

How do I use Getline with delimiter?

Using std::getline() in C++ to split the input using delimiters. We can also use the delim argument to make the getline function split the input in terms of a delimiter character. By default, the delimiter is \n (newline). We can change this to make getline() split the input based on other characters too!

Can you use Getline for characters?

The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered. Must read the article getline(string) in C++ for more details.


1 Answers

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`
}
like image 77
Some programmer dude Avatar answered Sep 30 '22 09:09

Some programmer dude