命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | io‎ | 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,
                     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 起)
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 基类和仅用于说明的数据成员 bufmode 初始化如下。

在初始化这些子对象后,重载 (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())是否初始化为 null 指针是实现定义的。
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 不是可复制构造的(CopyConstructible)

目录

[编辑] 参数

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

[编辑] 注解

通常由 std::basic_stringstream 的构造函数调用。

除了 std::ios_base::instd::ios_base::out 之外的打开模式的支持程度在不同实现中有所不同。C++11 明确指定了在 str() 和此构造函数中对 std::ios_base::ate 的支持,但 std::ios_base::appstd::ios_base::truncstd::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> 的公开成员函数) [编辑]
English Deutsch 日本語 한국어 中文(简体) 中文(繁體)