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

本地化库

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

平面类别

格式化数值为字符序列以输出

std::time_put
template<

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

> class num_put;

std::num_put 封装格式化数值为字符串的规则。具体而言支持 bool 、 long 、 unsigned long 、 long long 、 unsigned long long 、 double 、 long double 、 void* 类型和所有能隐式转换到它们的类型(例如 int 或 float )。标准格式化输出运算符(如 cout << n; )用 I/O 流的 locale 的 std::num_put 平面生成数字的文本表示。

继承图

类型要求

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

特化

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

定义于头文件 <locale>

std::num_put<char>创建数的窄字符串表示
std::num_put<wchar_t>创建数的宽字符串表示
std::num_put<char, OutputIt>用定制输出迭代器创建数的窄字符串表示
std::num_put<wchar_t, OutputIt>用定制输出迭代器创建数的宽字符串表示

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

成员类型

成员类型定义
char_typeCharT
iter_typeOutputIt

成员函数

(构造函数)

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

(析构函数)

销毁 num_put 平面
(受保护成员函数)

put

调用 do_put
(公开成员函数)

受保护成员函数

do_put

[虚]

格式化数字并写入到输出流
(虚受保护成员函数)

成员对象

static std::locale::id id

locale 的 id
(公开成员对象)

构造新的 num_put 平面

std::num_put<CharT,OutputIt>::num_put

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

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

参数

refs-开始的引用计数

销毁 num_put 平面

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

protected: ~num_put();

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

调用示例

 

#include <iostream>
#include <locale>

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

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

调用 do_put & 格式化数字并写入到输出流

std::num_put<CharT,OutputIt>::put, std::num_put<CharT,OutputIt>::do_put

public:
iter_type put( iter_type out, std::ios_base& str, char_type fill, bool v ) const;

(1)

iter_type put( iter_type out, std::ios_base& str, char_type fill, long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, long long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long long v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, double v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, long double v ) const;

iter_type put( iter_type out, std::ios_base& str, char_type fill, const void* v ) const;

protected:
virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, bool v ) const;

(2)

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long long v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long long ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, double v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long double v ) const;

virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, const void* v ) const;

1) 公开成员函数,调用最终导出类的受保护虚成员函数 do_put

2) 写字符到输出序列 out ,这些字符表示 v 的值,按格式化标志 str.flags() 和流 str 中感染的 locale 的 std::numpunct 和 std::ctype 平面所要求者格式化。此函数为所有有格式输出流运算符,如 std::cout << n; 所调用。

转换以四个阶段出现

阶段 1 :转换指定符选择
  • 获得 I/O 格式标志,如同以

fmtflags basefield = (str.flags() & std::ios_base::basefield);

fmtflags uppercase = (str.flags() & std::ios_base::uppercase);

fmtflags floatfield = (str.flags() & std::ios_base::floatfield);

fmtflags showpos = (str.flags() & std::ios_base::showpos);

fmtflags showbase = (str.flags() & std::ios_base::showbase);

fmtflags showpoint = (str.flags() & std::ios_base::showpoint);

  • v 的类型为 bool
    • 若 boolalpha == 0 ,则转换 v 为 int 类型并进行整数输出。
    • 若 boolalpha != 0 ,则若 v == true 则获得std::use_facet<std::numpunct<charT>>(str.getloc()).truename() ,或若 v == false 则获得 std::use_facet<std::numpunct<charT>>(str.getloc()).falsename() ,并以 *out++ = c 输出该字符串的每个相继字符 cout 。此情况下不进行进一步的操作,函数返回 out
  • v 的类型为整数类型,则选择下列首个可应用选项:

若 basefield == oct ,则将使用转换指定符 %o

若 basefield == hex && !uppercase ,则将使用转换指定符 %x

若 basefield == hex ,则将使用转换指定符 %X

v 的类型有符号,则将使用转换指定符 %d

v 的类型无符号,则将使用转换指定符 %u

  • 对于整数类型,若需要则添加长度修饰符到转换指定:对于 long 和 unsigned long 为 l ,对于 long long 和 unsigned long long 为 ll 。
  • v 为浮点类型,则选择首个可应用选项:

若 floatfield == std::ios_base::fixed ,则将使用转换指定符 %f

若 floatfield == std::ios_base::scientific && !uppercase ,则将使用转换指定符 %e

若 floatfield == std::ios_base::scientific ,则将使用转换指定符 %E

若 floatfield == (std::ios_base::fixed | std::ios_base::scientific) && !uppercase ,则将使用转换指定符 %a

若 floatfield == (std::ios_base::fixed | std::ios_base::scientific) ,则将使用转换指定符 %A

(C++11 起)

若 !uppercase ,则将使用转换指定符 %g

否则,将使用转换指定符 %G

  • v 的类型为 long double ,则添加长度修饰符 L 到转换指定符。
  • 另外若 floatfield != (ios_base::fixed | ios_base::scientific) 则 (C++11 起)添加设为 str.precision() 的精度指定符
  • 对于整数和浮点类型,若设置了 showpos ,则前附修饰符 + 。
  • 对于整数类型,若设置了 showbase ,则前附修饰符 # 。
  • 对于浮点类型,若设置了 showpoint ,则前附修饰符 # 。
  • v 的类型为 void* ,则将使用格式指定符 %p 。
  • 如同以在 "C" 本地环境中调用 std::printf(spec, v) 创建窄字符串,其中 spec 是所选的格式指定符。
阶段 2 :本地环境限定转换
  • 调用 std::use_facet<std::ctype<CharT>>(str.getloc()).widen(c) ,将阶段 1 中获得的每个异于小数点 '.' 的字符 c 转换为 CharT
  • 对于算术类型,按照 std::use_facet<std::numpunct<CharT>>(str.getloc()).grouping() 所提供的分组规则,插入从 std::use_facet<std::numpunct<CharT>>(str.getloc()).thousands_sep() 获得的千分隔字符到序列中。
  • 以 std::use_facet<std::numpunct<CharT>>(str.getloc()).decimal_point() 替换小数点字符( '.' )。
阶段 3 :填充
  • 如同以 std::fmtflags adjustfield = (flags & (std::ios_base::adjustfield)) 获得调整标志,并以如下方式检验它以鉴别填充位置

若 adjustfield == std::ios_base::left ,则将在后方填充

若 adjustfield == std::ios_base::right ,则将在前方填充

若 adjustfield == std::ios_base::internal 且表示中出现符号字符,则将在符号后填充

若 adjustfield == std::ios_base::internal 而阶段 1 表示以 0x 或 0X 开始,则将在 x 或 X 后填充

否则,将在前方填充

  • 若 str.width() 非零(例如刚使用了 std::setw )且阶段 2 后的 CharT 数小于 str.width() ,则插入 fill 字符的副本于填充所指示的位置,令序列长度达到 str.width() 。

任何情况下,都调用 str.width(0) 以取消 std::setw 的效果。

阶段 4 :输出

如同用 *out++ = c 输出来自阶段 3 的 CharT 序列的每个相继字符 c

参数

out-指向首个要写入字符的迭代器
str-取得格式化信息来源的流
fill-需要填充结果到域宽时使用的填充字符
v-转换为字符串并输出的值

返回值

out

注意

转换指定 #o (例如产生自 std::showbase 和 std::oct 的组合)不计为填充字符。

格式化浮点值为 hexfloat 时(即 floatfield == (std::ios_base::fixed | std::ios_base::scientific) 时),不使用流的精度;而是始终以足以准确表示值的精度打印该数。(C++11 起)

调用示例 windows

 

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#include <Windows.h>

std::vector<std::wstring> locals;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    locals.push_back(pStr);
    return TRUE;
}

std::string stows(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

// 此定制 num_put 平面输出所有整数类型的平方(除了 long long )
struct squaring_num_put : std::num_put<char>
{
    iter_type do_put(iter_type s, std::ios_base& f,
                     char_type fill, long v) const
    {
        return std::num_put<char>::do_put(s, f, fill, v * v);
    }

    iter_type do_put(iter_type s, std::ios_base& f,
                     char_type fill, unsigned long v) const
    {
        return std::num_put<char>::do_put(s, f, fill, v * v);
    }
};


int main()
{
    EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);

    for (std::vector<std::wstring>::const_iterator str = locals.begin();
            str != locals.end(); ++str)
    {
        std::locale::global(std::locale(stows(*str)));
        std::cout.imbue(std::locale(stows(*str)));
        std::cout << "The locale " << std::cout.getloc().name() << ' ';

        auto& facet = std::use_facet<std::num_put<char>>(std::cout.getloc());
        facet.put(std::cout, std::cout, '0', 2.71);
        std::cout << "          ";

        std::cout.imbue(std::locale(std::cout.getloc(), new squaring_num_put));
        std::cout << 6 << "     " << -12 << std::endl;
    }

    return 0;
}

输出

The locale de-DE_phoneb 2,71          36     144
The locale es-ES_tradnl 2,71          36     144
The locale hu-HU_technl 2,71          36     144
The locale ja-JP_radstr 2.71          36     144
The locale ka-GE_modern 2,71          36     144
The locale x-IV_mathan 2.71          36     144
The locale zh-CN_phoneb 2.71          36     144
The locale zh-CN_stroke 2.71          36     144
The locale zh-HK_radstr 2.71          36     144
The locale zh-MO_radstr 2.71          36     144
The locale zh-MO_stroke 2.71          36     144
The locale zh-SG_phoneb 2.71          36     144
The locale zh-SG_stroke 2.71          36     144
The locale zh-TW_pronun 2.71          36     144
The locale zh-TW_radstr 2.71          36     144

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值