c++11 标准模板(STL)(std::basic_ios)(四)

定义于头文件 <ios>

template<

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

> class basic_ios : public std::ios_base

类 std::basic_ios 提供设施,以对拥有 std::basic_streambuf 接口的对象赋予接口。数个 std::basic_ios 对象能指涉一个实际的 std::basic_streambuf 对象。

继承图

 

还提供了两个对常见的字符类型的特化:

类型定义
iosbasic_ios<char>
wiosbasic_ios<wchar_t>

格式化

复制格式化信息

std::basic_ios<CharT,Traits>::copyfmt

basic_ios& copyfmt(const basic_ios& other);

若 other 与 *this 指代同一对象,则无效果。否则复制流 other 的状态到 *this 中。以下列序列进行:

1) 传递 erase_event 为参数,调用 register_callback() 所注册的每个回调。

2) 从 other 复制所有成员对象到 *this ,除了 rdstate() 、异常掩码和 rdbuf() 。特别是复制本地环境、格式化标志、数组 std::ios_base::iword 和 std::ios_base::pword 的内容(但不是 iwordpword 指针本身)、回调和 tie 的流。

3) 传递 copyfmt_event为参数,调用 register_callback() 所注册的每个回调。

4) 从 other 复制异常掩码到 *this ,如同通过调用 exceptions(other.exceptions()) 。

参数

other-用作源的另一流

返回值

*this

注意

通过回调的第二趟可用于深复制 std::ios_base::pword 中的指针所指向的用户定义对象。

copyfmt() 可用于保存和恢复流状态。 Boost 为相同目的提供更加细粒度的 IO state savers 库。

调用示例

#include <iostream>
#include <fstream>

int main()
{
    std::ofstream out;

    out.copyfmt(std::cout); // 复制 rdstate 和 rdbuf 外的所有内容
    out.clear(std::cout.rdstate()); // 复制 rdstate
    out.basic_ios<char>::rdbuf(std::cout.rdbuf()); // 共享缓冲

    out << "Hello, world\n";
}

输出

 

管理填充字符

std::basic_ios<CharT,Traits>::fill

CharT fill() const;

(1)

CharT fill( CharT ch );

(2)

管理用于填充输入转换到指定宽度的填充字符。

1) 返回当前的填充字符

2) 设置填充字符为 ch ,返回填充字符的先前值

参数

ch-用作填充字符的字符

返回值

调用函数前的填充字符。

调用示例

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << "With default setting : " << std::setw(10) << 40 << '\n';
    char prev = std::cout.fill('x');
    std::cout << "Replaced '" << prev << "' with '"
              << std::cout.fill() << "': " << std::setw(10) << 40 << '\n';
}

输出

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值