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 | ||||
basic_stringbuf::basic_stringbuf | ||||
(C++11) | ||||
(C++11) | ||||
(C++20) | ||||
Protected member functions | ||||
Non-member functions | ||||
(C++11) | ||||
Exposition-only member functions | ||||
(1) | ||
explicit basic_stringbuf(std::ios_base::openmode which= std::ios_base::in|std::ios_base::out); | (until C++11) | |
explicit basic_stringbuf(std::ios_base::openmode which); | (since C++11) | |
basic_stringbuf() : basic_stringbuf(std::ios_base::in|std::ios_base::out){} | (2) | (since C++11) |
explicit basic_stringbuf(conststd::basic_string<CharT, Traits, Allocator>& s, | (3) | |
explicit basic_stringbuf(std::basic_string<CharT, Traits, Allocator>&& s, std::ios_base::openmode which= | (4) | (since C++20) |
basic_stringbuf(std::ios_base::openmode which,const Allocator& a); | (5) | (since C++20) |
explicit basic_stringbuf(const Allocator& a) : basic_stringbuf(std::ios_base::in|std::ios_base::out, a){} | (6) | (since C++20) |
template<class SAlloc> explicit basic_stringbuf(conststd::basic_string<CharT, Traits, SAlloc>& s, | (7) | (since C++20) |
template<class SAlloc> basic_stringbuf(conststd::basic_string<CharT, Traits, SAlloc>& s, | (8) | (since C++20) |
template<class SAlloc> basic_stringbuf(conststd::basic_string<CharT, Traits, SAlloc>& s, | (9) | (since C++20) |
template<class StringViewLike> explicit basic_stringbuf(const StringViewLike& t, | (10) | (since C++26) |
template<class StringViewLike> basic_stringbuf(const StringViewLike& t, | (11) | (since C++26) |
template<class StringViewLike> basic_stringbuf(const StringViewLike& t,const Allocator& a); | (12) | (since C++26) |
basic_stringbuf( basic_stringbuf&& rhs); | (13) | (since C++11) |
basic_stringbuf( basic_stringbuf&& rhs,const Allocator& a); | (14) | (since C++20) |
basic_stringbuf(const basic_stringbuf& rhs)= delete; | (15) | (since C++11) |
Thestd::basic_streambuf base and theexposition-only data membersbuf
andmode
are initialized as follows.
After initializing these subobjects, overloads(3-12) initialize the input and output sequences as if by callinginit_buf_ptrs()
.
Overload | std::basic_streambuf base | buf | mode |
---|---|---|---|
(1) | default-initialized | implementation-defined (see below) | which |
(2) | std::ios_base::in| std::ios_base::out | ||
(3) | s | which | |
(4) | std::move(s) | ||
(5) | a | ||
(6) | std::ios_base::in| std::ios_base::out | ||
(7) | s | which | |
(8) | {s, a} | ||
(9) | std::ios_base::in| std::ios_base::out | ||
(10) | {sv, Allocator()} | which | |
(11) | {sv, a} | ||
(12) | std::ios_base::in| std::ios_base::out | ||
(13) | rhs (copy constructed) | std::move(rhs).str() | rhs.mode |
(14) | {std::move(rhs).str(), a} |
Contents |
s | - | astd::basic_string used to initialize the buffer | ||||||||||||||||
t | - | an object (convertible tostd::basic_string_view) used to initialize the buffer | ||||||||||||||||
a | - | another allocator used to construct the internalstd::basic_string | ||||||||||||||||
rhs | - | anotherbasic_stringbuf | ||||||||||||||||
which | - | specifies stream open mode. It is bitmask type, the following constants are defined:
|
Typically called by the constructor ofstd::basic_stringstream.
The level of support for the open modes other thanstd::ios_base::in andstd::ios_base::out varies among implementations. C++11 explicitly specifies the support forstd::ios_base::ate instr() and in this constructor, butstd::ios_base::app,std::ios_base::trunc, andstd::ios_base::binary have different effects on different implementations.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_sstream_from_string_view | 202306L | (C++26) | Interfacing string streams withstd::string_view |
Demonstrates calling the constructor ofstd::basic_stringbuf
directly:
#include <iostream>#include <sstream> int main(){// default constructor (mode = in | out)std::stringbuf buf1; buf1.sputc('1');std::cout<<&buf1<<'\n'; // string constructor in at-end mode (C++11)std::stringbuf buf2("test",std::ios_base::in|std::ios_base::out|std::ios_base::ate); buf2.sputc('1');std::cout<<&buf2<<'\n'; // append mode test (results differ among compilers)std::stringbuf buf3("test",std::ios_base::in|std::ios_base::out|std::ios_base::app); buf3.sputc('1'); buf3.pubseekpos(1); buf3.sputc('2');std::cout<<&buf3<<'\n';}
Output:
1test1est12 (Sun Studio) 2st1 (GCC)
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) allocated no array object 2. overload(3) did not specify how the input and output sequences are initialized | 1. removed the limitation 2. specified |
LWG 562 | C++98 | overload(3) setepptr() to point one past the last underlying character ifbool(which&std::ios_base::out)==true | epptr() can be set beyond that position |
P0935R0 | C++11 | the default constructor was explicit | made implicit |
constructs the string stream (public member function of std::basic_stringstream<CharT,Traits,Allocator> )[edit] |