命名空间
变体
操作

std::money_get

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

    class CharT,
    class InputIt = std::istreambuf_iterator<CharT>

> class money_get;

类模板 std::money_get 封装了从字符流解析货币值的规则。标准 I/O 操纵符 std::get_money 使用 I/O 流的区域设置的 std::money_get facet。

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

继承关系图

如果标准库不保证提供 std::money_get 特化(见下文),则其 get()do_get() 的行为不保证如指定。

内容

[编辑] 特化

标准库保证提供以下特化(它们是任何区域设置对象都必须实现的

定义于头文件 <locale>
std::money_get<char> 解析货币值的窄字符串表示形式
std::money_get<wchar_t> 解析货币值的宽字符串表示形式

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

[编辑] 嵌套类型

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

[编辑] 数据成员

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

[编辑] 成员函数

构造一个新的 money_get facet
(公共成员函数)
调用 do_get
(公共成员函数)

[编辑] 受保护的成员函数

析构一个 money_get facet
(受保护的成员函数)
[虚拟]
从输入流解析货币值
(虚拟受保护的成员函数) [编辑]

[编辑] 示例

#include <iomanip>
#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
 
int main()
{
    std::string str = "$1.11 $2.22 $3.33";
    std::cout << std::fixed << std::setprecision(2);
 
    std::cout << '\"' << str << "\" parsed with the I/O manipulator: ";
    std::istringstream s1(str);
    s1.imbue(std::locale("en_US.UTF-8"));
 
    long double val;
    while (s1 >> std::get_money(val))
        std::cout << val / 100 << ' ';
    std::cout << '\n';
 
    str = "USD  1,234.56";
    std::cout << '\"' << str << "\" parsed with the facet directly: ";
    std::istringstream s2(str);
    s2.imbue(std::locale("en_US.UTF-8"));
 
    auto& f = std::use_facet<std::money_get<char>>(s2.getloc());
    std::ios_base::iostate err;
    std::istreambuf_iterator<char> beg(s2), end;
    f.get(beg, end, true, s2, err, val);
 
    std::cout << val / 100 << '\n';
}

输出

"$1.11 $2.22 $3.33" parsed with the I/O manipulator: 1.11 2.22 3.33
"USD  1,234.56" parsed with the facet directly: 1234.56

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 427 C++98 money_get 保证接受任何 CharT,它
满足字符的要求,在该字符上
可以实例化任何 iostream 组件
仅保证接受 char
wchar_t 和其他实现
定义的字符类型
LWG 2392 C++98 只有字符类型 CharT 可以
保证被 money_get 接受
可以保证接受实现
定义的字符容器类型

[编辑] 参见

定义了由 std::money_getstd::money_put 使用的货币格式化参数
(类模板) [编辑]
将货币值格式化为字符序列以进行输出
(类模板) [编辑]
(C++11)
解析货币值
(函数模板) [编辑]