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>::operator=

std::basic_stringbuf& operator=( std::basic_stringbuf&& rhs );

(1)(C++11 起)

std::basic_stringbuf& operator=( const std::basic_stringbuf& rhs ) = delete;

(2)

1) 移动赋值运算符:移动 rhs 的内容到 *this 中。移动后 *this 拥有 rhs 之前保有的关联 string 、打开模式、本地环境和所有其他状态。保证 *this 中 std::basic_streambuf 的六个指针有别于被移动的 rhs 的对应指针,除非它们为空。

2) 复制赋值运算符被删除; basic_stringbuf可复制赋值 (CopyAssignable) 。

参数

rhs-将被移动的另一 basic_stringbuf

返回值

*this

 调用示例

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    std::basic_stringbuf<char> one("one", std::ios_base::in
                                   | std::ios_base::out
                                   | std::ios_base::ate);

    std::basic_stringbuf<char> two("two", std::ios_base::in
                                   | std::ios_base::out
                                   | std::ios_base::ate);

    std::cout << "Before move, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"" << std::endl;

    //1) 移动赋值运算符:移动 rhs 的内容到 *this 中。
    //移动后 *this 拥有 rhs 之前保有的关联 string 、打开模式、本地环境和所有其他状态。
    //保证 *this 中 std::basic_streambuf 的六个指针有别于被移动的 rhs 的对应指针,除非它们为空。
    one = std::move(two);

    std::cout << "After move, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"" << std::endl;

    return 0;
}
输出

交换二个 basic_stringbuf 对象

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

void swap( std::basic_stringbuf& rhs )

(C++11 起)

交换 *this 和 rhs 的状态与内容。

参数

rhs-另一 basic_stringbuf

返回值

(无)

注意

交换 std::stringstream 对象时自动调用此函数,很少需要直接调用它。

调用示例

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    std::basic_stringbuf<char> one("one", std::ios_base::in
                                   | std::ios_base::out
                                   | std::ios_base::ate);

    std::basic_stringbuf<char> two("two", std::ios_base::in
                                   | std::ios_base::out
                                   | std::ios_base::ate);

    std::cout << "Before move, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"" << std::endl;

    //交换 *this 和 rhs 的状态与内容。
    one.swap(two);

    std::cout << "After swap, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"" << std::endl;

    return 0;
}
输出

 

替换或获得关联字符串的副本

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

std::basic_string<CharT, Traits, Allocator> str() const;

(1)

void str( const std::basic_string<CharT, Traits, Allocator>& s);

(2)

获取和设置底层字符串。

1) 创建并返回保有此 std::basic_stringbuf 底层字符序列副本的 std::basic_string 。对于仅输入流,返回的字符串含来自范围 [eback(), egptr()) 的字符。对于输入/输出或仅输出流,含有 pbase() 到序列中末字符的字符,不考虑 egptr() 和 epptr() 。

为写入打开的缓冲区中的成员字符序列能为效率目的过分配。该情况下,仅返回初始化的字符:从构造函数 string 参数获得的字符、最近对 str() 的第二重载的 string 参数的字符或来自写入操作的字符。典型的使用过分配的实现维护一个高水位指针,以跟踪缓冲区已初始化部分的结尾,而此重载返回从 pbase() 到高水位指针的字符。

(C++11 起)

2) 删除此 std::basic_stringbuf 的整个底层字符序列,然后配置新的含有 s 内容副本的底层字符序列。以下列方式初始化 std::basic_streambuf 的指针:

  • 对于输入流( mode & ios_base::in == true ), eback() 指向首字符, gptr() == eback() ,而 egptr() == eback() + s.size() :后继输入将读取首个复制自 s 的字符。
  • 对于输出流( mode & ios_base::out == true ), pbase() 指向首字符而 epptr() >= pbase() + s.size() (允许 epptr 指向更远以令后随的 sputc() 不会立即调用 overflow()
    • 对于后附流( mode & ios_base::ate == true ), pptr() == pbase() + s.size() ,从而后继输出将被后附到复制自 s 的最后字符。(C++11 起)
    • 对于非后附输出流, pptr() == pbase() ,从而后继输出将重写复制自 s 的字符。

参数

s-保有替换字符序列的 string 对象

返回值

1) 保有此缓冲的底层字符序列副本的 string 对象。

2) (无)

注意

此函数典型地通过 std::basic_stringstream::str() 访问。

 调用示例

#include <sstream>
#include <string>
#include <iostream>

int main()
{
    char n;

    std::basic_stringbuf<char> in;  // 亦能用 in("1 2")
    in.str("1 2"); // 设置获取区
    n = in.sgetc();
    std::cout << "after reading the first char from \"1 2\", the char is "
              << n << ", str() = \"" << in.str() << "\"" << std::endl; // 或 in.str()

    std::basic_stringbuf<char> out("1 2");
    out.sputc('3');
    std::cout << "after writing the char '3' to output stream \"1 2\""
              << ", str() = \"" << out.str() << "\"" << std::endl;

    return 0;
}
输出

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值