c++11 标准模板(STL)(std::basic_stringbuf)(二)

定义于头文件 <sstream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>

> class basic_stringbuf : public std::basic_streambuf<CharT, Traits>

 std::basic_stringbuf 是关联字符序列为内存常驻的任意字符序列的 std::basic_streambuf 。能从 std::basic_string 的实例初始化它,或将它做成该类的实例。

std::basic_stringbuf 的典型实现保有一个 std::basic_string 类型对象,或等价的可伸缩序列容器作为数据成员,并将它同时用作受控制字符序列(为 std::basic_streambuf 的六个指针所指向的数组)和关联字符序列(所有输入操作的字符源和输出操作的目标)。

另外,典型的实现保有一个 std::ios_base::openmode 类型的数据成员,以指示流的状态(只读、只写、读写、尾端写等)。

若 overflow() 使用过分配策略,则可存储另外的高水位指针,以跟踪最后初始化的字符。(C++11 起)

亦提供二个对常用字符类型的特化:

类型定义
stringbufbasic_stringbuf<char>
wstringbufbasic_stringbuf<wchar_t>


公开成员函数

 构造一个 basic_stringbuf 对象

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

basic_stringbuf() : basic_stringbuf(std::ios_base::in | std::ios_base::out) { }

(1)

explicit basic_stringbuf( std::ios_base::openmode which =

std::ios_base::in | std::ios_base::out );

(2)(C++11 前)

explicit basic_stringbuf( std::ios_base::openmode which );

(C++11 起)

explicit basic_stringbuf( const std::basic_string<CharT, traits, Allocator>& new_str, 

std::ios_base::openmode which = std::ios_base::in| std::ios_base::out );

(3)

basic_stringbuf( const basic_stringbuf& rhs ) = delete;

(4)(C++11 起)

basic_stringbuf( basic_stringbuf&& rhs );

(5)(C++11 起)

1) 默认构造函数。是否初始化序列指针 (eback()gptr()egptr()pbase()pptr()epptr()) 为空指针是实现定义的。

2) 构造 std::basic_stringbuf 对象:通过调用 std::basic_streambuf 的默认构造函数初始化基类,以空字符串初始化字符序列,并设置模式为 which

3) 通过进行同 1) 中的的操作构造 std::basic_stringbuf 对象,随后如同通过调用 str(new_str) 初始化关联字符序列。

4) 复制构造函数被删除; std::basic_stringbuf可复制构造 (CopyConstructible) 。

5) 通过从另一 std::basic_stringbuf 对象 rhs 移动所有状态,包含关联 string 、打开模式、 locale 和所有其他状态,移动构造 std::basic_stringbuf 对象。移动后,保证 *this 中 std::basic_streambuf 的六个指针异于被移动的 rhs 中的指针,除非它们为空。

参数

new_str-用于初始化缓冲区的 basic_string
rhs-另一 basic_stringbuf
which-指定流打开模式。它是位掩码类型,定义下列常量:
常量解释
app每次写入前寻位到流结尾
binary以二进制模式打开
in为读打开
out为写打开
trunc在打开时舍弃流的内容
ate打开后立即寻位到流结尾

注意

常由 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 在不同实现上有不同效果。

 调用示例

#include <iostream>
#include <sstream>

int main()
{
    // 默认构造函数( mode = in|out )
    std::basic_stringbuf<char> basic_stringbuf1;
    basic_stringbuf1.sputc('1');
    std::cout << "basic_stringbuf1: " << &basic_stringbuf1 << std::endl;

    // 在尾端模式中的 string 构造函数 (C++11)
    std::basic_stringbuf<char> basic_stringbuf2("test", std::ios_base::in
            | std::ios_base::out
            | std::ios_base::ate);
    basic_stringbuf2.sputc('1');
    std::cout << "basic_stringbuf2: " << &basic_stringbuf2 << std::endl;

    // 后附模式测试(结果在编译器间有别)
    std::basic_stringbuf<char> basic_stringbuf3("test", std::ios_base::in
            | std::ios_base::out
            | std::ios_base::app);
    basic_stringbuf3.sputc('1');
    basic_stringbuf3.pubseekpos(1);
    basic_stringbuf3.sputc('2');
    std::cout << "basic_stringbuf3: " << &basic_stringbuf3 << std::endl;

    //5) 通过从另一 std::basic_basic_stringbuf 对象 rhs 移动所有状态,
    //包含关联 string 、打开模式、 locale 和所有其他状态,
    //移动构造 std::basic_basic_stringbuf 对象。
    std::basic_stringbuf<char> basic_stringbuf4(std::move(basic_stringbuf2));
    std::cout << "basic_stringbuf4: " << &basic_stringbuf4 << std::endl;

    return 0;
}
输出

 

析构函数

析构 basic_stringbuf 对象和其所保有的 string

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值