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

定义于头文件 <streambuf>
template<

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

> class basic_streambuf;

basic_streambuf 控制字符序列的输入与输出。它包含下列内容并提供到它们的访问:

1) 受控制字符序列,又称为缓冲区,它可含有为输入操作缓冲的输入序列(又称为获取区),和/或为输出操作缓冲的输出序列(又称为放置区)。

2) 关联字符序列,又称作(对于输入)或(对于输出)。它可以是通过 OS API 访问的实体(文件、 TCP 接头、串行端口、其他字符设备),或者可以是能转译成字符源或池的对象( std::vector 、数组、字符串字面量)。

I/O 流对象 std::basic_istream 及 std::basic_ostream ,还有所有导出自它们的对象( std::ofstream 、 std::stringstream 等),都完全以 std::basic_streambuf 实现。

成员函数

析构 basic_streambuf 对象

std::basic_streambuf<CharT,Traits>::~basic_streambuf

virtual ~basic_streambuf();

此析构函数为空:此 basic_streambuf 的成员(指针和本地环境)与此析构函数返回后的通常对象析构序列一致的顺序析构。然而,因为它被声明为公开虚函数,故通过指向基类的指针删除允许导出自 std::basic_streambuf 的对象。

参数

(无)

受保护成员函数

构造 basic_streambuf 对象

std::basic_streambuf<CharT,Traits>::basic_streambuf

protected:
basic_streambuf();

(1)

protected:
basic_streambuf(const basic_streambuf& rhs);

(2)(C++11 起)

1) 构造 basic_streambuf 对象,初始化六个成员( eback()gptr()egptr()pbase()pptr()epptr() )为空指针值, locale 成员为 std::locale() ,构造时的全局 C++ 本地环境的副本。

2) 从 rhs 复制构造,初始化六个指针和 locale 对象为 rhs 所保有值的副本。注意这是浅复制:新构造的 basic_streambuf 的指针指向的字符数组与 rhs 的相同。

两个构造函数均为受保护,而且仅为具体的 streambuf 类,如 std::basic_filebuf 、 std::basic_stringbuf 或 std::strstreambuf 调用。

参数
rhs-要复制的 streambuf 对象

注意

C++11 前, basic_streambuf 或其任何导出类是否可复制构造 (CopyConstructible) 是未指定的( LWG 问题 421 ),而不同的 C++ 库实现提供不同的选项。

替换 basic_streambuf 对象

std::basic_streambuf<CharT,Traits>::operator=

basic_streambuf& operator=( const basic_streambuf& other );

(C++11 起)

赋值 other 的数据成员给 *this 。

参数
other-要复制的流缓冲
返回值

*this

异常

(无)

交换二个 basic_streambuf 对象

std::basic_streambuf<CharT,Traits>::swap

void swap( basic_streambuf& other );

(C++11 起)

交换流缓冲与 other 的内容。

参数
other-要交换内容的流缓冲
返回值

(无)

异常

(无)

调用示例

#include <iostream>
#include <fstream>

int main()
{
    std::filebuf* fbp = new std::filebuf;
    fbp->open("test.txt", std::ios_base::out);
    fbp->sputn("Hello\n", 6);
    std::streambuf* sbp = fbp;
    delete sbp; // 关闭文件,冲入并写入输出
    std::ifstream f("test.txt");
    std::cout << f.rdbuf(); // 证明
}

 输出

受保护成员函数

本地环境

调用 imbue()

std::basic_streambuf<CharT,Traits>::pubimbue, 
std::basic_streambuf<CharT,Traits>::imbue

std::locale pubimbue( const std::locale& loc );

(1)

protected:
virtual void imbue( const std::locale& loc );

(2)

更改关联的本地环境。

1) 设置 loc 为本地环境。调用最终导出类的 imbue(loc)

2) 此函数的基类版本无效果。导出类可覆写此函数以提示本地环境的更改。最终导出类可以在 imbue() 的调用间缓存本地环境和成员平面。

参数
loc-要关联的 locale 对象
返回值

1) 先前关联的本地环境。

 

获得相关本地环境的副本

std::basic_streambuf<CharT,Traits>::getloc

std::locale getloc() const;

返回关联的本地环境。

关联的本地环境是在最后一次调用 pubimbue() 时提供的值,或若未调用过该函数,则为在构造 streambuf 时全局本地环境的值( std::locale )。

参数

(无)

返回值

关联的本地环境。

更改关联的本地环境

std::basic_streambuf<CharT,Traits>::pubimbue, 
std::basic_streambuf<CharT,Traits>::imbue

std::locale pubimbue( const std::locale& loc );

(1)

protected:
virtual void imbue( const std::locale& loc );

(2)

更改关联的本地环境。

1) 设置 loc 为本地环境。调用最终导出类的 imbue(loc)

2) 此函数的基类版本无效果。导出类可覆写此函数以提示本地环境的更改。最终导出类可以在 imbue() 的调用间缓存本地环境和成员平面。

参数
loc-要关联的 locale 对象
返回值

1) 先前关联的本地环境。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值