More about stringstream
Now it's time to get back to our discussion of stringstream. A stringstream is very similar to an ostream, except that once we have written data to a stringstream, we can read it from the stringstream into a variable just as though we were reading from a file or the keyboard. An example is the program shown in Figure 9.20, which uses a stringstream object to combine a year, month, and day number to make one string containing all of those values.
Figure 9.20. A stringstream formatting example (code\stream2.cpp)
#include <iostream> #include <sstream> using namespace std; int main() { stringstream FormatStream; string date; short year = 1996; short month = 7; short day = 28; FormatStream << year; FormatStream << month; FormatStream ... |
Get C++: A Dialog Programming with the C++ Standard Library now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.