To return or assign the basic_string object stored in the buffer.
basic_string<charT> str() const; void str(const basic_string<charT> &s);
The function str() freezes the buffer then returns a basic_string object.
The function str(const string s) assigns the value of the string `s' to the stringbuf object.
The no argument version returns a basic_string if successful. The function with an argument has no return.
basic_stringbuf::str(), basic_istringstream.str()
basic_stringstream::str()
#include <iostream> #include <sstream> int main() { using namespace std; ostringstream out; out << "Ask the teacher anything\n"; out << "OK, what is 2 + 2?\n"; out << 2 << " plus " << 2 << " equals " << 4 << ends; cout << out.str(); return 0; }
Result:
Ask the teacher anything OK, what is 2 + 2? 2 plus 2 equals 4?