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 起) |
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 的交互 |
示例
演示直接调用
std::basic_stringbuf
构造函数:
#include <iostream> #include <sstream> int main() { // 默认构造函数 (mode = in | out) std::stringbuf buf1; buf1.sputc('1'); std::cout << &buf1 << '\n'; // 字符串构造函数,使用 at-end 模式 (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>
的公开成员函数)
|