line = "hello 2021"
istringstream split_string(line);
was a useful code to get the values from a string, i.e.
split_string>>str>>temp_double;
However, suppose that the value of line was changed, i.e.
line="hello world"
,and one wanted to write the following code with the updated value of line.
split_string>>str1>>str2;
How to update split_string so that it streamed the updated value of line?
In the C++20 standard
std::istringstreamnow has anstr()method that allows replacing the contents of its internal buffer. This functionality is not available in C++17 or earlier.Before C++20 the closest alternative would be to use
std::stringstreaminstead ofstd::istringstreamand use<<to put stuff into the string stream that can be read back out.CORRECTION: it possible to use
str()to set the contents ofstd::istringstreamprior to C++20, so this is the simplest solution.