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} |
eback()、gptr()、egptr()、pbase()、pptr()、epptr())是否初始化为 null 指针是实现定义的。std::basic_string_view<CharT, Traits>> 为 true 时才参与重载决议。
*this 中的六个序列指针是否获得 rhs 的值是实现定义的。rhs 为空但可用,并且- 令 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 | - | a 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 的接口 |
[编辑] 示例
演示直接调用 std::basic_stringbuf 的构造函数
#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'; }
输出
1 test1 est12 (Sun Studio) 2st1 (GCC)
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
|---|---|---|---|
| LWG 432 | C++98 | 1. 重载 (1) 未分配数组对象 2. 重载 (3) 未指定如何初始化输入 和输出序列 |
1. 移除了限制 2. 已指定 |
| LWG 562 | C++98 | 如果 bool(which & std::ios_base::out) == true,则重载 (3) 将 epptr() 设置为指向最后一个底层字符之后的位置如果 bool(which & std::ios_base::out) == true |
epptr() 可以设置超出该位置 |
| P0935R0 | C++11 | 默认构造函数是 explicit 的 | 改为隐式 |
[编辑] 参阅
| 构造字符串流 ( std::basic_stringstream<CharT,Traits,Allocator> 的公开成员函数) |