命名空间
变体
操作

std::money_get

来自 cppreference.cn
< cpp‎ | 本地化
 
 
 
 
 
定义于头文件 <locale>
模板<

    CharT,
    InputIt = std::istreambuf_iterator<CharT>

> money_get;

类模板 std::money_get 封装了从字符流中解析货币值的规则。标准 I/O 操作符 std::get_money 使用 I/O 流本地化设置中的 std::money_get 方面。

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 [静态] facet 的标识符

[编辑] 成员函数

构造一个新的 money_get 方面
(公开成员函数)
调用 do_get
(公开成员函数)

[编辑] 受保护成员函数

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

[编辑] 示例

#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++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
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)
解析货币值
(函数模板) [编辑]