命名空间
变体
操作

std::money_put

来自 cppreference.cn
< cpp‎ | locale
 
 
 
 
 
定义于头文件 <locale>
template<

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

> class money_put;

std::money_put 封装了将货币值格式化为字符串的规则。标准 I/O 操作符 std::put_money 使用 I/O 流区域设置的 std::money_put facet。

cpp/locale/locale/facetstd-money put-inheritance.svg

继承图

如果标准库不保证提供 std::money_put 特化(见下文),则其 put()do_put() 的行为不保证符合规定。

目录

[编辑] 特化

标准库保证提供以下特化(它们 要求由任何区域设置对象实现

定义于头文件 <locale>
std::money_put<char> 创建货币值的窄字符串表示
std::money_put<wchar_t> 创建货币值的宽字符串表示

此外,标准库还保证提供满足以下类型要求的每个特化:

[编辑] 嵌套类型

类型 定义
char_type CharT
string_type std::basic_string<CharT>
iter_type OutputIt

[编辑] 数据成员

成员 描述
std::locale::id id [static] facet 的标识符

[编辑] 成员函数

构造新的 money_put facet
(公开成员函数)
调用 do_put
(公开成员函数)

[编辑] 受保护成员函数

销毁 money_put facet
(受保护成员函数)
[virtual]
格式化货币值并写入输出流
(虚受保护成员函数) [编辑]

[编辑] 示例

#include <iomanip>
#include <iostream>
#include <iterator>
#include <locale>
 
int main()
{
    // using the I/O manipulator
    std::cout.imbue(std::locale("en_US.UTF-8"));
    std::cout << "American locale: "
              << std::showbase << std::put_money(12345678.9) << '\n';
 
    // using the facet directly
    std::cout.imbue(std::locale("de_DE.UTF-8"));
    std::cout << "German locale: ";
    auto& f = std::use_facet<std::money_put<char>>(std::cout.getloc());
    f.put({std::cout}, false, std::cout, std::cout.fill(), 12345678.9);
    std::cout << '\n';
}

输出

American locale: $123,456.79
German locale: 123.456,79 €

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 427 C++98 money_put 保证接受任何 CharT
满足作为字符的要求
任何 iostream 组件都可以在其上实例化
只保证接受 char,
wchar_t 和其他实现-
定义的字符类型
LWG 2392 C++98 只有字符类型 CharT 可以
保证被 money_put 接受
可以保证接受实现-
定义的字符容器类型

[编辑] 另请参见

定义 std::money_getstd::money_put 使用的货币格式化参数
(类模板) [编辑]
从输入字符序列解析和构造货币值
(类模板) [编辑]
(C++11)
格式化并输出货币值
(函数模板) [编辑]