I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Public member functions | ||||
(C++11) | ||||
(C++11) | ||||
basic_stringbuf::str | ||||
(C++20) | ||||
Protected member functions | ||||
Non-member functions | ||||
(C++11) | ||||
Exposition-only member functions | ||||
(1) | ||
std::basic_string<CharT, Traits, Allocator> str()const; | (until C++20) | |
std::basic_string<CharT, Traits, Allocator> str()const&; | (since C++20) | |
template<class SAlloc> std::basic_string<CharT, Traits, SAlloc> str(const SAlloc& a)const; | (2) | (since C++20) |
std::basic_string<CharT, Traits, Allocator> str()&&; | (3) | (since C++20) |
void str(conststd::basic_string<CharT, Traits, Allocator>& s); | (4) | |
template<class SAlloc> void str(conststd::basic_string<CharT, Traits, SAlloc>& s); | (5) | (since C++20) |
void str(std::basic_string<CharT, Traits, Allocator>&& s); | (6) | (since C++20) |
template<class StringViewLike> void str(const StringViewLike& t); | (7) | (since C++26) |
Gets and sets the underlying string.
In the descriptions below,buf andmode areexposition-only data members of*this.
std::basic_stringbuf
's underlying character sequence. For input-only streams, the returned string contains the characters from the range[
eback(),
egptr())
. For input/output or output-only streams, contains the characters frompbase() to the last character in the sequence regardless ofegptr() andepptr().str()
, or from a write operation. A typical implementation that uses over-allocation maintains a high-watermark pointer to track the end of the initialized part of the buffer and this overload returns the characters frompbase() to the high-watermark pointer.
| (since C++20) |
SAlloc
meets the requirements ofAllocator.init_buf_ptrs()
, then returns thestd::basic_string object.Allocator
.init_buf_ptrs()
.Contents |
s | - | astd::basic_string object holding the replacement character sequence |
t | - | an object (convertible tostd::basic_string_view) holding the replacement character sequence |
a | - | allocator to use for all memory allocations of the returnedstd::basic_string |
This function is typically accessed throughstd::basic_istringstream::str(),std::basic_ostringstream::str(), orstd::basic_stringstream::str().
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_sstream_from_string_view | 202306L | (C++26) | Interfacing string streams withstd::string_view |
#include <iostream>#include <sstream> int main(){int n; std::istringstream in;// could also use in("1 2") in.rdbuf()->str("1 2");// set the get area in>> n;std::cout<<"after reading the first int from\"1 2\", the int is "<< n<<", str() =\""<< in.rdbuf()->str()<<"\"\n";// or in.str() std::ostringstream out("1 2"); out<<3;std::cout<<"after writing the int '3' to output stream\"1 2\""<<", str() =\""<< out.str()<<"\"\n"; std::ostringstream ate("1 2",std::ios_base::ate);// C++11 ate<<3;std::cout<<"after writing the int '3' to append stream\"1 2\""<<", str() =\""<< ate.str()<<"\"\n";}
Output:
after reading the first int from "1 2", the int is 1, str() = "1 2"after writing the int '3' to output stream "1 2", str() = "3 2"after writing the int '3' to append stream "1 2", str() = "1 23"
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 432 | C++98 | 1. overload(1) did not specify the content of the underlying character sequence 2. overload(4) did not specify how the input and output sequences are initialized | both specified |
LWG 562 | C++98 | overload(4) setepptr() to point one past the last underlying character ifbool(mode&std::ios_base::out)==true | epptr() can be set beyond that position |
gets or sets the contents of underlying string device object (public member function of std::basic_stringstream<CharT,Traits,Allocator> )[edit] | |
(C++20) | obtains a view over the underlying character sequence (public member function)[edit] |