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

本地化库

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

平面类别

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

std::money_put
template<

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

> class money_put;

std::money_put 封装格式化货币值为字符串所用的规则。标准 I/O 操纵符 std::put_money 使用 I/O 流的 locale 的 std::money_put 平面。

继承图

类型要求

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

特化

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

定义于头文件 <locale>

std::money_put<char>创建货币值的窄字符串表示
std::money_put<wchar_t>创建货币值的宽窄字符串表示
std::money_put<char, OutputIt>用定制输入迭代器创建货币值的窄字符串表示
std::money_put<wchar_t, OutputIt>用定制输入迭代器创建货币值的宽字符串表示

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

成员类型

成员类型定义
char_typeCharT
string_typestd::basic_string<CharT>
iter_typeOutputIt

成员函数

(构造函数)

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

(析构函数)

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

put

调用 do_put
(公开成员函数)

受保护成员函数

do_put

[虚]

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

成员对象

static std::locale::id id

locale 的 id
(公开成员对象)

构造新的 money_put 平面

构造新的 money_put 平面

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

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

参数

refs-开始的引用计数

销毁 money_put 平面

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

protected: ~money_put();

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

调用示例

#include <iostream>
#include <locale>

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

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

格式化货币值并写入到输出流

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

iter_type put(iter_type out, bool intl, std::ios_base& f,

              char_type fill, long double quant) const;
(1)

iter_type put(iter_type out, bool intl, std::ios_base& f,
             char_type fill, const string_type& quant) const;

(2)
protected:

virtual iter_type do_put(iter_type out, bool intl, std::ios_base& str,

                         char_type fill, long double units) const;
(3)

virtual iter_type do_put(iter_type out, bool intl, std::ios_base& str,
                         char_type fill, const string_type& digits) const;

(4)

格式化货币值并写结果到输出流。

1-2) 公开成员函数,调用最终导出类的成员函数 do_put

3) 如同用 ct.widen(buf1, buf1 + std::sprintf(buf1, "%.0Lf", units), buf2) 转换数值参数 units 为宽字符串,其中 ct 为 str.getloc() 中感染的 std::ctype 平面,而 buf1buf2 是充分大的字符缓冲区。按后述方式处理、格式化结果字符串 buf2 并输出到 out

4) 从字符串参数 digits 采取仅有的可选前导负号(以与 ct.widen('-') 比较确定,其中 ct 为 str.getloc() 中感染的 std::ctype 平面)和立即后随的数位字符(以 ct 分类)为字符序列,按后述方式处理、格式化,并输出到 out

给定来自先前步骤的字符序列,若首字符等于 ct.widen('-') ,则调用 mp.neg_format() 获得格式化 pattern ,否则调用 mp.pos_format() ,其中 mp 为 str.getloc() 中感染的 std::moneypunct<CharT, intl> 平面。

mp.grouping()mp.frac_digits()mp.decimal_point()mp.thousands_sep() 所要求插入千分隔符和小数点,而将结果字符串置于输出序列中的 value 出现于格式化模式中的位置。

若 str.flags() & str.showbase 非零(使用了 std::showbase 操纵符),则通过调用 mp.curr_symbol() 生成通货符号或字符串,并将它置于输出序列中的 symbol 出现于格式化模式中的位置。

若 mp.positive_sign() (使用正格式模式的情况下)或 mp.negative_sign() (使用负格式模式的情况下)返回带有多于一个字符的字符串,则将首字符置于输出序列中的 sign 出现于格式化模式中的位置,而将剩余字符置于所有其他字符之后,例如格式化模式 {sign, value, space, symbol} 用 123 单位和 "-" 的 negative_sign 会导致 "-1.23 €" ,而 "()" 的 negative_sign 会生成 "(1.23 €)" 。

若为指定格式生成的字符数小于 str.width() 的返回值,则以如下方式,插入 fill 的副本以令输出序列的总长度准确达到 str.width() :

  • 若 str.flags() & str.adjustfield 等于 str.internal ,则插入填充字符到 nonespace 出现于格式化模式中的位置。
  • 否则,若 str.flags() & str.adjustfield 等于 str.left ,则后附 fill 的副本到所有其他字符后
  • 否则,将填充字符置于所有其他字符前characters.

最后,调用 str.width(0) 取消任何 std::setw 的效果。

返回值

指向最后产生字符立即后方的迭代器。

注意

假设通货单位为货币的最小非小数单位:美国中为美分,日本中为日元。

调用示例 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;
}

struct my_punct : std::moneypunct_byname<char, false>
{
    my_punct(const char* name) : moneypunct_byname(name) {}

    string_type do_negative_sign() const
    {
        return "()";
    }
};

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 locale(stows(*str));
        std::cout.imbue(locale);

        long double units = -123.45;
        std::cout << "In Russian locale, " << units << " prints as "
                  << std::showbase;
        // 注意:以下等价于简单的 std::put_money(units)
        std::use_facet<std::money_put<char>>(locale).put(
        {std::cout}, false, std::cout, std::cout.fill(), units);
        std::cout << std::endl;

        std::cout.imbue(std::locale(std::cout.getloc(), new my_punct(locale.name().c_str())));
        std::cout << "With negative_sign set to \"()\", it prints  as ";
        std::use_facet<std::money_put<char>>(locale).put(
        {std::cout}, false, std::cout, std::cout.fill(), units);
        std::cout << std::endl;
    }

    return 0;
}

输出

In Russian locale, -123,45 prints as -1,23 €
With negative_sign set to "()", it prints  as (1,23 €)
In Russian locale, -123,45 prints as -1,23 €
With negative_sign set to "()", it prints  as (1,23 €)
In Russian locale, -123,45 prints as -1,23 Ft
With negative_sign set to "()", it prints  as (1,23 Ft)
In Russian locale, -123.45 prints as -\123
With negative_sign set to "()", it prints  as (\123)
In Russian locale, -123,45 prints as -1,23 ?
With negative_sign set to "()", it prints  as (1,23 ?)
In Russian locale, -123.45 prints as -?.23
With negative_sign set to "()", it prints  as (?.23)
In Russian locale, -123.45 prints as ¥-1.23
With negative_sign set to "()", it prints  as ¥(1.23)
In Russian locale, -123.45 prints as ¥-1.23
With negative_sign set to "()", it prints  as ¥(1.23)
In Russian locale, -123.45 prints as -HK$1.23
With negative_sign set to "()", it prints  as (HK$1.23)
In Russian locale, -123.45 prints as -MOP1.23
With negative_sign set to "()", it prints  as (MOP1.23)
In Russian locale, -123.45 prints as -MOP1.23
With negative_sign set to "()", it prints  as (MOP1.23)
In Russian locale, -123.45 prints as -$1.23
With negative_sign set to "()", it prints  as ($1.23)
In Russian locale, -123.45 prints as -$1.23
With negative_sign set to "()", it prints  as ($1.23)
In Russian locale, -123.45 prints as -NT$1.23
With negative_sign set to "()", it prints  as (NT$1.23)
In Russian locale, -123.45 prints as -NT$1.23
With negative_sign set to "()", it prints  as (NT$1.23)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值