Difference between revisions of "Istringstream"
Line 1: | Line 1: | ||
− | '''<code>istringstream</code>''' can be used to turn any <code>string</code> into a stream. This can be useful for parsing a <code>string</code> using the extraction operator <code>>><code> or the <code>getline<code> function. | + | '''<code>istringstream</code>''' can be used to turn any <code>string</code> into a stream. This can be useful for parsing a <code>string</code> using the extraction operator <code>>></code> or the <code>getline</code> function. |
To use <code>istringstream</code> you must <code>#include <sstream></code>. | To use <code>istringstream</code> you must <code>#include <sstream></code>. |
Latest revision as of 13:57, 27 January 2018
istringstream
can be used to turn any string
into a stream. This can be useful for parsing a string
using the extraction operator >>
or the getline
function.
To use istringstream
you must #include <sstream>
.
This code snippet will output 15, 25, and 35 on separate lines:
string s = "10 20 30";
istringstream iss(s);
while (iss)
{
int x;
iss >> x;
cout << "Result: " << x + 5 << endl;
}
Note that using istringstream
to convert a single value to a numerical format is overkill and instead functions like stoi
or stod
should be used instead.