string str() const;
void str (const string& s);
Get/set content
The first form (1) returns a string object with a copy of the current contents of the stream.
The second form (2) sets s as the contents of the stream, discarding any previous contents. The object preserves its open mode: if this includes ios_base::ate, the writing position is moved to the end of the new sequence.
Internally, the function calls the str member of its internal string buffer object.
// stringstream::str
#include // std::string
#include // std::cout
#include // std::stringstream, std::stringbuf
int main () {
std::stringstream ss;
ss.str (“Example string”);
std::string s = ss.str();
std::cout << s << ‘\n’;
return 0;
}
输出 Example string