c++11 标准模板(STL)本地化库 - 平面类别 (std::moneypunct_byname)表示系统提供的具名本地环境的 std::moneypunct

本地化库

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

平面类别

表示系统提供的具名本地环境的 std::moneypunct

std::moneypunct_byname

template< class CharT, bool Intl = false >
class moneypunct_byname : public std::moneypunct<CharT, Intl>;

std::moneypunct_byname 是 std::moneypunct 平面,封装在其构造时指定的 locale 的货币格式化偏好。

标准库提供二个特化

定义于头文件 <locale>

std::moneypunct_byname<char, Intl>窄字符 I/O 的本地环境限定 std::moneypunct 平面
std::moneypunct_byname<wchar_t, Intl>宽字符 I/O 的本地环境限定 std::moneypunct 平面

成员类型

成员类型定义
patternstd::money_base::pattern
string_typestd::basic_string<CharT>

成员函数

(构造函数)

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

(析构函数)

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

std::moneypunct_byname::moneypunct_byname

explicit moneypunct_byname( const char* name, std::size_t refs = 0 );

explicit moneypunct_byname( const std::string& name, std::size_t refs = 0 );

(C++11 起)

为名为 name 的本地环境构造新的 std::moneypunct_byname 平面。

refs 用于资源管理:在销毁最后一个保有平面的 std::locale 对象时,若 refs == 0 ,则实现销毁平面对象。否则,不销毁对象。

参数

name-本地环境的名称
refs-链接到该平面的引用数

std::moneypunct_byname::~moneypunct_byname

protected:
~moneypunct_byname();

销毁平面。

继承自 std::moneypunct

成员类型

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

成员函数

decimal_point

调用 do_decimal_point
(std::moneypunct<CharT,International> 的公开成员函数)

thousands_sep

调用 do_thousands_sep
(std::moneypunct<CharT,International> 的公开成员函数)

grouping

调用 do_grouping
(std::moneypunct<CharT,International> 的公开成员函数)

curr_symbol

调用 do_curr_symbol
(std::moneypunct<CharT,International> 的公开成员函数)

positive_signnegative_sign

调用 do_positive_signdo_negative_sign
(std::moneypunct<CharT,International> 的公开成员函数)

frac_digits

调用 do_frac_digits
(std::moneypunct<CharT,International> 的公开成员函数)

pos_formatneg_format

调用 do_pos_format/do_neg_format
(std::moneypunct<CharT,International> 的公开成员函数)

受保护成员函数

do_decimal_point

[虚]

提供用作小数点的字符
(std::moneypunct<CharT,International> 的虚受保护成员函数)

do_thousands_sep

[虚]

提供用作千分隔符的字符
(std::moneypunct<CharT,International> 的虚受保护成员函数)

do_grouping

[虚]

提供二个千分隔符间的位数
(std::moneypunct<CharT,International> 的虚受保护成员函数)

do_curr_symbol

[虚]

提供用作通货标识符的字符串
(std::moneypunct<CharT,International> 的虚受保护成员函数)

do_positive_signdo_negative_sign

[虚]

提供指示正或负值的字符串
(std::moneypunct<CharT,International> 的虚受保护成员函数)

do_frac_digits

[虚]

提供小数点后要显示的位数
(std::moneypunct<CharT,International> 的虚受保护成员函数)

do_pos_formatdo_neg_format

[虚]

提供通货值的格式化模式
(std::moneypunct<CharT,International> 的虚受保护成员函数)

成员常量

成员定义
const bool intl [静态]International

成员对象

static std::locale::id id

locale 的 id
(公开成员对象)

继承自 std::money_base

成员类型定义
enum part { none, space, symbol, sign, value };无作用域枚举类型
struct pattern { char field[4]; };货币格式类型
枚举常量解释
none容许但不要求空白符,除了在末位置不容许空白符
space要求一或多个空白字符
symbol要求 moneypunct::curr_symbol 所返回的字符序列
sign要求 moneypunct::positive_sign 或 moneypunct::negative_sign 所返回的首个字符
value要求绝对数值货币值

调用示例 windows

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#include <exception>
#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_moneypunct_byname : std::moneypunct_byname<char>
{
    explicit my_moneypunct_byname(const char *_Locname, size_t _Refs = 0)
        : my_moneypunct_byname(_Locname, _Refs)
    {
        // construct for named locale
    }

    explicit my_moneypunct_byname(const std::string& _Str, size_t _Refs = 0)
        : moneypunct_byname(_Str.c_str(), _Refs)
    {
        // construct for named locale
    }

    //提供通货值的格式化模式
    pattern do_pos_format()  const override
    {
        return { {value, space, symbol, sign} };
    }

    //提供通货值的格式化模式
    pattern do_neg_format() const override
    {
        return { {value, space, symbol, sign} };
    }

    //提供小数点后要显示的位数
    int do_frac_digits()     const override
    {
        return 1;
    }

    //提供用作千分隔符的字符
    char_type do_thousands_sep() const override
    {
        return ',';
    }

    //提供二个千分隔符间的位数
    string_type do_grouping()    const override
    {
        return "\003";
    }

    //提供用作通货标识符的字符串
    string_type do_curr_symbol() const override
    {
        return "¥";
    }

    //提供用作小数点的字符
    char_type do_decimal_point() const override
    {
        return '.';
    }

    //提供指示负值的字符串
    string_type do_negative_sign() const override
    {
        return "(-)";
    }

    //提供指示正值的字符串
    string_type do_positive_sign() const override
    {
        return "(+)";
    }
};

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

    for (std::vector<std::wstring>::const_iterator str = locals.begin();
            str != locals.end(); ++str)
    {
        if (stows(*str) == "x-IV_mathan")
        {
            continue;
        }

        std::locale locale(stows(*str));
        std::cout.imbue(locale);

        std::cout << locale.name() << " ";

        std::cout.imbue(locale);
        std::cout << "american locale: " << std::showbase
                  << std::put_money(1234.5) << std::endl;

        std::cout.imbue(std::locale(std::cout.getloc(), new my_moneypunct_byname(stows(*str))));
        std::cout << "locale with modified moneypunct: "
                  << std::put_money(12345678.0) << std::endl;
    }

    return 0;
}

输出

de-DE_phoneb american locale: 12,35 €
locale with modified moneypunct: 1,234,567.8 ¥(+)
es-ES_tradnl american locale: 12,35 €
locale with modified moneypunct: 1,234,567.8 ¥(+)
hu-HU_technl american locale: 12,35 Ft
locale with modified moneypunct: 1,234,567.8 ¥(+)
ja-JP_radstr american locale: \1,235
locale with modified moneypunct: 1,234,567.8 ¥(+)
ka-GE_modern american locale: 12,35 ?
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-CN_phoneb american locale: ¥12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-CN_stroke american locale: ¥12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-HK_radstr american locale: HK$12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-MO_radstr american locale: MOP12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-MO_stroke american locale: MOP12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-SG_phoneb american locale: $12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-SG_stroke american locale: $12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-TW_pronun american locale: NT$12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)
zh-TW_radstr american locale: NT$12.35
locale with modified moneypunct: 1,234,567.8 ¥(+)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值