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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/612419.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

聊聊ChatGPT:智能语言模型背后的原理

目录 1. ChatGPT的基础&#xff1a;GPT模型 2. 预训练与微调&#xff1a;让模型更加智能 2.1 预训练 2.2 微调 3. 多样化的应用场景 4. 未来的展望 5. 结语 在当今的人工智能领域&#xff0c;OpenAI的ChatGPT无疑是一个炙手可热的话题。它不仅能流畅地进行对话&#xff…

【ArcGISProSDK】condition属性

示例 通过caption属性可以看出esri_mapping_openProjectCondition的条件是一个工程被打开 condition的作用 由此可知示例中的Tab实在工程被打开才能使用&#xff0c;否则他禁用显示灰色&#xff0c;在未禁用的时候说明条件满足。 参考文档 insertCondition 元素 (arcgis.com…

局域网手机端远程控制手机

局域网手机端远程控制手机 随着科技的进步和智能设备的普及&#xff0c;远程控制技术在日常生活与工作中的应用越来越广泛。其中&#xff0c;局域网内的手机端远程控制手机技术&#xff0c;因其便捷性和实用性&#xff0c;受到了众多用户的关注。本文将简要介绍该技术及其应用…

#兼职副业赚钱吗?# 宝妈与上班族在水牛社的财富探索

在这个繁忙的都市节奏中&#xff0c;宝妈与上班族都面临着平衡家庭与经济的挑战。那么&#xff0c;兼职副业真的能为他们带来额外的收入吗&#xff1f;接下来&#xff0c;让我们通过两个实例&#xff0c;揭示宝妈和上班族是如何在水牛社找到兼职副业赚钱的契机的。 ✨ 宝妈的故…

Prompt|Kimi高阶技巧,99%的人都不知道

大家好&#xff0c;我是无界生长。 今天分享一条咒语&#xff0c;轻松让Kimi帮你生成流程图&#xff0c;学会了的话&#xff0c;点赞收藏起来吧&#xff01; 效果展示 我们演示一下让kimi帮忙绘制 关注微信公众号“无界生长”的流程图&#xff0c;最终效果图如下所示 效果还不…

Dijkstra求最短路 I:图解 详细代码(图解)

文章目录 题目&#xff1a;Dijkstra求最短路思路伪代码&#xff1a;代码优化优化代码&#xff1a;Java代码 总结 题目&#xff1a;Dijkstra求最短路 给定一个 n个点 m条边的有向图&#xff0c;图中可能存在重边和自环&#xff0c;所有边权均为正值。 请你求出 1号点到 n号点的…

权限束缚术--权限提升你需要知道这些

前言 欢迎来到我的博客 个人主页:北岭敲键盘的荒漠猫-CSDN博客 本文主要对渗透测试中权限提升的一些基础知识进行整理 并不包含权限提升的具体操作 适合要入门权限提升的朋友 提权的重要性 我们在渗透网站时&#xff0c;我们往往会拿到一些权限&#xff0c;但是我们的权限有…

视频批量剪辑指南:一键合并视频并添加背景音乐,高效便捷

在数字化时代&#xff0c;视频剪辑已经成为了一项常见且重要的技能。无论是制作家庭影片、工作展示还是社交媒体内容&#xff0c;掌握高效的视频剪辑技巧都能极大地提升我们的工作效率和创作质量。本文将为您介绍云炫AI智剪中高效的视频批量剪辑方法&#xff0c;让您能够一键合…

ubantu安装docker以及docker-compose

ubantu安装docker以及docker-compose 安装docker1、从官方存储库中安装Docker2、启动Docker服务3、验证 安装docker compose使用docker部署服务1、需要再opt文件夹下创建以下文件夹&#xff0c;/opt文件夹目录说明2、可将已备份对应文件夹拷至对应文件夹下3、在/opt/compose目录…

linux打包流程

因为linux有俩个python版本&#xff0c;我们需要切换到python3这个版本&#xff0c;默认是python 2.7 alias pythonpython3 切换到python3 再次执行&#xff1a;python -V 显示出python的版本了&#xff0c;然后查看pip的配置&#xff0c;我们打包里面需要的第三方需要放到pip…

Gradient发布支持100万token的Lllama3,上下文长度从8K扩展到1048K

前言 近日Gradient公司在Crusoe Energy公司的算力支持下&#xff0c;开发了一款基于Llama-3的大型语言模型。这款新模型在原Llama-3 8B的基础上&#xff0c;将上下文长度从8000 token大幅扩展到超过104万token。 这一创新性突破&#xff0c;展现了当前SOTA大语言模型在长上下…

OC foudation框架(上)学习

foundation框架 文章目录 foundation框架字符串&#xff08;NSString && NSMutableString&#xff09;NSString的其他功能NSMutableString 日期与时间 &#xff08;NSDate&#xff09;2.1 日期与时间&#xff08;NSDate&#xff09;2.2日期格式器日历与日期组件定时器&…

霍金《时间简史 A Brief History of Time》书后索引(A--D)

图源&#xff1a;Wikipedia INDEX A Abacus Absolute position Absolute time Absolute zero Acceleration Age of the universe Air resistance Albrecht, Andreas Alpha Centauri Alpher, Ralph Anthropic principle Antigravity Antiparticles Aristotle Arrows of time …

【spring】Security 密码加密算法

Spring Security 提供了多种密码加密算法&#xff0c;用于在存储用户密码时进行加密&#xff0c;以增强安全性。 查看org.springframework.security.crypto.factory.PasswordEncoderFactories 以下是一些常用的密码加密算法&#xff1a; BCryptPasswordEncoder&#xff1a; 这…

gin框架学习笔记(二) ——相关数据与文件的响应

前言 在看是今天的内容之前&#xff0c;我们收先来探究一下&#xff1a;什么是Web应用工作的原理&#xff1f;当然这个问题其实论述起来是很麻烦的&#xff0c;但是我们将它无限的缩小&#xff0c;其实可以简化为一个C/S模型&#xff0c;客户端(Client)负责发送请求&#xff0…

基于ambari hdp的kafka用户授权读写权限

基于ambari hdp的kafka用户授权读写权限 版本Kafka 2.0.0添加自定义配置修改admin密码重启kafka授权读取授权写入有效通配符部分举例 版本Kafka 2.0.0 添加自定义配置 authorizer.class.name kafka.security.auth.SimpleAclAuthorizer super.users User:admin allow.everyo…

UE5 C++软引用

一.软引用通常是仅储存资源对象的资源路径没有与资源产生耦合关系的引用&#xff08;软引用加载到内存中&#xff0c;只有在需要的时候才会被加载&#xff09; 软引用通常有FSoftObjectPath、FSoftClassPath、TSoftObjectPtr、TSoftClassPtr。它指向的资源未被加载&#xff0c…

es关闭开启除了系统索引以外的所有索引

1、es 开启 “删除或关闭时索引名称支持通配符” 功能 2、kibanan平台执行 POST *,-.*/_close 关闭索引POST *,-.*/_open 打开索引3、其他命令 DELETE index_* // 按通配符删除以index_开头的索引 DELETE _all // 删除全部索引 DELETE *,-.* 删除全…

Ansible-Playbook通过role角色部署Lnmp架构

目录​​​​​​​ 一、配置Nginx的Roles角色 1.1编写files/default.conf 1.2编写files/nginx.repo 1.3编写handlers/main.yml 1.4编写tasks/main.yml文件 1.5编写vars/main.yml文件 1.6测试运行结果&#xff0c;并不是真的执行任务 二、配置Mysql的Roles角色 2.1编写…

物联网D3——按键控制LED、光敏传感蜂鸣器

按键控制LED 按键抖动&#xff0c;电平发生变化&#xff0c;可用延时函数抵消按键抖动对系统的影响 传感器电路图 按键电路图 c语言对应类型 “_t”后缀表示使用typedef重命名的数据类型 枚举类型 #include<iostream> using namespace std; //定义枚举类型 typedef enu…