std::basic_stringbuf<CharT,Traits,Allocator>::basic_stringbuf
| (1) | ||
|   explicit basic_stringbuf( std::ios_base::openmode which = std::ios_base::in | std::ios_base::out );  | 
(C++11 前) | |
|   explicit basic_stringbuf( std::ios_base::openmode which );  | 
(C++11 起) | |
|   basic_stringbuf() : basic_stringbuf( std::ios_base::in | std::ios_base::out ) {}  | 
(2) | (C++11 起) | 
|   explicit     basic_stringbuf( const std::basic_string<CharT, Traits, Allocator>& s,  | 
(3) | |
|   explicit basic_stringbuf( std::basic_string<CharT, Traits, Allocator>&& s,                           std::ios_base::openmode which =  | 
(4) | (C++20 起) | 
|   basic_stringbuf( std::ios_base::openmode which, const Allocator& a );  | 
(5) | (C++20 起) | 
|   explicit basic_stringbuf( const Allocator& a ) : basic_stringbuf( std::ios_base::in | std::ios_base::out, a ) {}  | 
(6) | (C++20 起) | 
|   template< class SAlloc > explicit basic_stringbuf( const std::basic_string<CharT, Traits, SAlloc>& s,  | 
(7) | (C++20 起) | 
|   template< class SAlloc > basic_stringbuf( const std::basic_string<CharT, Traits, SAlloc>& s,  | 
(8) | (C++20 起) | 
|   template< class SAlloc > basic_stringbuf( const std::basic_string<CharT, Traits, SAlloc>& s,  | 
(9) | (C++20 起) | 
|   template< class StringViewLike > explicit basic_stringbuf( const StringViewLike& t,  | 
(10) | (C++26 起) | 
|   template< class StringViewLike > basic_stringbuf( const StringViewLike& t,  | 
(11) | (C++26 起) | 
|   template< class StringViewLike > basic_stringbuf( const StringViewLike& t, const Allocator& a );  | 
(12) | (C++26 起) | 
|   basic_stringbuf( basic_stringbuf&& rhs );  | 
(13) | (C++11 起) | 
|   basic_stringbuf( basic_stringbuf&& rhs, const Allocator& a );  | 
(14) | (C++20 起) | 
|   basic_stringbuf( const basic_stringbuf& rhs ) = delete;  | 
(15) | (C++11 起) | 
std::basic_streambuf 基以及仅用于阐述的数据成员 buf 和 mode 会按以下方式初始化。
在初始化这些子对象后,重载 (3-12) 如同以调用 init_buf_ptrs() 来初始化输入和输出序列。
| 重载 | std::basic_streambuf 基 | buf
 | 
mode
 | 
|---|---|---|---|
| (1) | 默认初始化 | 由实现定义 (见下文)  | 
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 (复制构造)  | 
std::move(rhs).str() | rhs.mode | 
| (14) | {std::move(rhs).str(), a} | 
std::basic_string_view<CharT, Traits>> 是 true 时才会参与重载决议。
- 以 rhs_p 作为 rhs 在此次构造前一刻的状态,以下表达式都会求值为 true:
 
- str() == rhs_p.str()
 - getloc() == rhs_p.getloc()
 - gptr() - eback() == rhs_p.gptr() - rhs_p.eback()
 - egptr() - eback() == rhs_p.egptr() - rhs_p.eback()
 - pptr() - pbase() == rhs_p.pptr() - rhs_p.pbase()
 - epptr() - pbase() == rhs_p.epptr() - rhs_p.pbase()
 
- 以 rhs_a 作为 rhs 在此次构造后一刻的状态,以下表达式都会求值为 true:
 
- !eback() || eback() != rhs_a.eback()
 - !gptr() || gptr() != rhs_a.gptr()
 - !egptr() || egptr() != rhs_a.egptr()
 - !pbase() || pbase() != rhs_a.pbase()
 - !pptr() || pptr() != rhs_a.pptr()
 - !epptr() || epptr() != rhs_a.epptr()
 
参数
| s | - | 用于初始化缓冲区的 std::basic_string | ||||||||||||||||
| t | - | 用于初始化缓冲区的对象(可转换到 std::basic_string_view) | ||||||||||||||||
| a | - | 用于构造内部 std::basic_string 的另一分配器 | ||||||||||||||||
| rhs | - |  另一 basic_stringbuf
 | ||||||||||||||||
| which | - |  指定流打开模式。它是位掩码类型,定义下列常量:
  | 
注解
常由 std::basic_stringstream 的构造函数调用。
std::ios_base::in 和 std::ios_base::out 以外的打开模式支持级别在实现中各有不同。C++11 显式指定 str() 中和此构造函数中支持 std::ios_base::ate,但 std::ios_base::app、std::ios_base::trunc 和 std::ios_base::binary 在不同实现上有不同效果。
| 功能特性测试宏 | 值 | 标准 | 注释 | 
|---|---|---|---|
__cpp_lib_sstream_from_string_view | 
202306L | (C++26) | 字符串流的 std::string_view 接口 | 
示例
演示直接调用 basic_stringbuf 的构造函数。
#include <iostream> #include <sstream> int main() { // 默认构造函数(mode = in | out) std::stringbuf buf1; buf1.sputc('1'); std::cout << &buf1 << '\n'; // 在尾端模式中的字符串构造函数(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'; // 后附模式测试(结果在编译器间有别) 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'; }
输出:
1 test1 est12 (Sun Studio) 2st1 (GCC)
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 432 | C++98 |  1. 重载 (1) 不会分配数组对象 2. 重载 (3) 没有指定如何初始化输入和输出序列  | 
 1. 移除该限制 2. 已指定  | 
| LWG 562 | C++98 |  重载 (3) 在 bool(which & std::ios_base::out) == true 时会设置 epptr() 到指向最后一个底层字符的下一位置  | 
epptr() 可以设置到更后面的位置 | 
| P0935R0 | C++11 | 默认构造函数是显式的 | 改成隐式的 | 
参阅
|   构造字符串流  ( std::basic_stringstream<CharT,Traits,Allocator> 的公开成员函数)  |