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>

受保护成员函数

用绝对寻址,重定位输入序列、输出序列或两者中的下一位置指针

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

virtual pos_type seekpos(pos_type sp,

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

若可能,则重寻位 std::basic_streambuf::gptr 和/或 std::basic_streambuf::pptr 到 sp 所指示的位置。

等效地执行 seekoff(off_type(sp), std::ios_base::beg, which) 。

参数

sp-流位置,例如由 seekoff() 或 seekpos() 获得者
which-定义影响的是输入序列、输出序列或两者。它能为下列常量之一或其组合:
常量解释
in影响输入序列
out影响输出序列

返回值

成功时为 sp ,失败时为 pos_type(off_type(-1)) 。

注意

seekpos() 为 std::basic_streambuf::pubseekpos() 所调用,它又为 std::basic_istream::seekg() 和 std::basic_ostream::seekp() 的单参数版本所调用。

 调用示例

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

//  typedef basic_stringbuf<char> 	stringbuf;
struct mybuf : std::stringbuf
{
    mybuf(const std::string& new_str,
          std::ios_base::openmode which =
              std::ios_base::in | std::ios_base::out)
        : std::stringbuf(new_str, which) {}

    pos_type tellp()
    {
        char ch = *pptr();
        return ch;
    }

    pos_type tellg()
    {
        char ch = *gptr();
        return ch;
    }

    pos_type seekpos(pos_type sp, std::ios_base::openmode which)
    {
        std::cout << "Before seekpos(" << sp
                  << "), size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr()
                  << " read positions available" << std::endl;

        pos_type rc = std::stringbuf::seekpos(sp, which);

        std::cout << "seekpos() returns " << rc
                  << ".\nAfter the call, "
                  << "size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr()
                  << " read positions available" << std::endl;

        return rc;
    }

};

int main()
{
    mybuf buf("12345");
    std::iostream stream(&buf);
    stream.seekg(2);

    return 0;
}

非成员函数

特化 std::swap 算法

std::swap(std::basic_stringbuf)
template< class CharT, class Traits, class Alloc >

void swap( std::basic_stringbuf<CharT,Traits,Alloc>& lhs,

           std::basic_stringbuf<CharT,Traits,Alloc>& rhs );
(C++11 起)

为 std::basic_stringbuf 特化 std::swap 算法。交换 lhsrhs 的内部状态。等效地调用 lhs.swap(rhs) 。

参数

lhs, rhs-要交换状态的 std::basic_stringbuf 对象

返回值

(无)

 调用示例

#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;

    //为 std::basic_stringbuf 特化 std::swap 算法。
    //交换 lhs 与 rhs 的内部状态。等效地调用 lhs.swap(rhs) 。
    std::swap(one, two);

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

    return 0;
}
输出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值