From cppreference.com
char*str();(deprecated in C++98)
(removed in C++26)Calls
, then returns a copy of start pointer of the get area,
.
The start of the get area, for all writeable std::strstreambuf objects constructed through the interface provided by
, is also the start of the put area.
Parameters
(none)
Return value
A copy of
, which may be a null pointer.
Notes
This function is typically called through the
interface.
The call to
guarantees that the returned pointer remains valid until the next explicit call to
(false): otherwise (on a dynamic buffer) any output operation could trigger buffer reallocation which would invalidate the pointer. It also causes a memory leak in the destructor of std::strstreambuf, unless freeze(false) is called before the buffer (or, more commonly, the std::strstream that manages it) is destroyed.
Example
Run this code
#include<iostream>#include<strstream>intmain(){std::strstreamdyn;// dynamically-allocated read/write bufferdyn<<"Test: "<<1.23<<std::ends;std::strstreambuf*buf=dyn.rdbuf();std::cout<<"R/W buffer holds ["<<buf->str()// or dyn.str()<<"]\n";dyn.freeze(false);// after calling .str() on a dynamic strstreamchararr[10];std::ostrstreamuser(arr,10);// fixed-size write-only bufferbuf=user.rdbuf();user<<1.23<<std::ends;std::cout<<"Write-only buffer holds ["<<buf->str()// or user.str()<<"]\n";std::istrstreamlit("1 2 3");// fixed-size read-only bufferbuf=lit.rdbuf();std::cout<<"Read-only buffer holds ["<<buf->str()// or lit.str()<<"]\n";}Output:
R/W buffer holds [Test: 1.23] Write-only buffer holds [1.23] Read-only buffer holds [1 2 31 2 3] See also
accesses the output buffer
(public member function of std::strstream)
accesses the output buffer
(public member function of std::ostrstream)
accesses the output buffer
(public member function of std::istrstream)