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
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.
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.
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!
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.
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`
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With