To make the output more readable, we can insert text between the variables:
#include <iostream> #include <sstream> int main() //from w w w . j a v a 2s. c o m { char c = 'A'; int x = 123; double d = 456.78; std::stringstream ss; ss << "The char is: " << c << ", int is: "<< x << " and double is: " << d; std::cout << ss.str(); }
To output data from a stream into an object, we use the >> operator:
#include <iostream> #include <sstream> #include <string> int main() /*from ww w .j a v a2 s . c om*/ { std::string s = "A 123 456.78"; std::stringstream ss{ s }; char c; int x; double d; ss >> c >> x >> d; std::cout << c << ' ' << x << ' ' << d << ' '; }
This example reads/outputs data from a string stream into our variables.