Namespaces
Variants

std::basic_stringbuf<CharT,Traits,Allocator>:: basic_stringbuf

From cppreference.net
<tr class="t-dcl t
(1)
(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,
std:: ios_base :: openmode which =

std:: ios_base :: in | std:: ios_base :: out ) ;
(3)
explicit basic_stringbuf ( std:: basic_string < CharT, Traits, Allocator > && s,

std:: ios_base :: openmode which =

std:: ios_base :: in | std:: ios_base :: out ) ;
(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,
std:: ios_base :: openmode which =

std:: ios_base :: in | std:: ios_base :: out ) ;
(7) (C++20 起)
template < class SAlloc >

basic_stringbuf ( const std:: basic_string < CharT, Traits, SAlloc > & s,

std:: ios_base :: openmode which, const Allocator & a ) ;
(8) (C++20 起)
template < class SAlloc >

basic_stringbuf ( const std:: basic_string < CharT, Traits, SAlloc > & s,
const Allocator & a )

: basic_stringbuf ( s, std:: ios_base :: in | std:: ios_base :: out , a ) { }
(9) (C++20 起)
template < class StringViewLike >

explicit basic_stringbuf ( const StringViewLike & t,
std:: ios_base :: openmode which =

std:: ios_base :: in | std:: ios_base :: out ) ;
(10) (C++26 起)
template < class StringViewLike >

basic_stringbuf ( const StringViewLike & t,

std:: ios_base :: openmode which, const Allocator & a ) ;
(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 }
1,2) 重载 (1) (C++11 前) (2) (C++11 起) 是默认构造函数。序列指针( eback() gptr() egptr() pbase() pptr() epptr() )是否初始化为空指针由实现定义。
5,6) 构造完成后, str. empty ( ) true
7) 仅当 std:: is_same_v < SAlloc, Allocator > false 时,此重载才参与重载决议。
10-12) 隐式转换 t 为字符串视图 sv ,如同通过 std:: basic_string_view < CharT, Traits > sv = t ; 实现,随后按照上表中的方式使用。
这些重载仅当 std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true 时参与重载决议。
13,14) 重载 (13) 是移动构造函数。 * 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 ( )
15) 复制构造函数被删除; std::basic_stringbuf 不满足 可复制构造 要求。

目录

参数

s - 用于初始化缓冲区的 std::basic_string
t - 用于初始化缓冲区的对象(可转换为 std::basic_string_view
a - 用于构造内部 std::basic_string 的另一个分配器
rhs - 另一个 basic_stringbuf
which - 指定流打开模式。它是位掩码类型,定义了以下常量:
常量 说明
app 每次写入前定位到流末尾
binary 二进制模式 打开
in 为读取打开
out 为写入打开
trunc 打开时清空流内容
ate 打开后立即定位到流末尾
noreplace (C++23) 以独占模式打开

注释

通常由 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> 的公开成员函数)