Namespaces
Variants

std:: money_get

From cppreference.net
定义于头文件 <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/facet std-money get-inheritance.svg

继承关系图

如果标准库不保证提供某个 std::money_get 特化(参见下文),则其 get() do_get() 的行为不保证符合规范说明。

目录

特化

标准库保证提供以下特化(它们 是任何locale对象必须实现的 ):

定义于头文件 <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 facet
(公开成员函数)
调用 do_get
(公开成员函数)

受保护成员函数

析构 money_get facet
(受保护成员函数)
[virtual]
从输入流解析货币值
(虚受保护成员函数)

示例

#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 被保证接受任何满足
iostream组件实例化要求的
CharT 字符类型
仅保证接受 char ,
wchar_t 及其他实现
定义的字符类型
LWG 2392 C++98 仅字符类型 CharT 可被
保证被 money_get 接受
可保证接受实现
定义的字符容器类型

另请参阅

定义由 std::money_get std::money_put 使用的货币格式化参数
(类模板)
将货币值格式化为字符序列输出
(类模板)
(C++11)
解析货币值
(函数模板)