c++11 标准模板(STL)本地化库 - 平面类别(time_put) - 格式化 std::tm 内容为字符序列以输出

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

从输入字符序列中解析时间/日期值到 std::tm 中

std::time_put
template<

    class CharT,
    class OutputIt = std::ostreambuf_iterator<CharT>

> class time_put;

类模板 std::time_put 封装日期和时间格式化规则。 I/O 操纵符 std::put_time 用 I/O 流的 locale 的 std::time_put 平面生成 std::tm 对象的文本表示。

继承图

类型要求

- OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。

特化

标准库提供二个(独立于本地环境)的全特化和二个部分特化:

定义于头文件 <locale>

std::time_put<char> 创建日期和时间的窄字符串表示
std::time_put<wchar_t> 创建日期和时间的宽字符串表示
std::time_put<char, OutputIt> 用定制输出迭代器创建日期和时间的窄字符串表示
std::time_put<wchar_t, OutputIt> 用定制输出迭代器创建日期和时间的宽字符串表示

另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。

成员类型

成员类型 定义
char_type CharT
iter_type OutputIt

 

成员函数

(构造函数)

构造新的 time_put 平面
(公开成员函数)

(析构函数)

析构 time_put 平面
(受保护成员函数)

put

调用 do_put
(公开成员函数)

成员对象

static std::locale::id id

locale 的 id
(公开成员对象)

受保护成员函数

do_put

[虚]

格式化日期/时间并写入输出流
(虚受保护成员函数)

构造新的 time_put 平面

std::time_put<CharT,OutputIt>::time_put

explicit time_put( std::size_t refs = 0 );

创建 std::time_put 平面并转发引用计数 refs 到基类构造函数 locale::facet::facet() 。

参数

refs - 开始的引用计数

析构 time_put 平面

std::time_put<CharT,OutputIt>::~time_put

protected: ~time_put();

析构 std::time_put 平面。此析构函数为受保护且为虚(由于基类析构函数为虚)。 std::time_put 类型对象,同大多数平面,只能在最后一个实装此平面的 std::locale 离开作用域时,或若用户定义导出自 std::time_put 并实现公开构造函数,才会被销毁。

调用示例

#include <iostream>
#include <locale>

struct Destructible_time_put : public std::time_put<wchar_t>
{
    Destructible_time_put(std::size_t refs = 0) : time_put(refs) {}
    // 注意:隐式析构函数为公开
};

int main()
{
    Destructible_time_put dc;
    // std::time_put<wchar_t> c;  // 编译错误:受保护析构函数
    return 0;
}


调用 do_put & 格式化日期/时间并写入输出流

std::time_put<CharT,OutputIt>::put, 
std::time_put<CharT,OutputIt>::do_put
public:

iter_type put( iter_type out, std::ios_base& str,
               char_type fill, const std::tm* t,

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值