命名空间
变体
操作

std::money_get<CharT,InputIt>::get, do_get

来自 cppreference.cn
< cpp‎ | locale‎ | money get
 
 
 
 
std::money_get
成员函数
money_get::getmoney_get::do_get
 
定义于头文件 <locale>
public:

iter_type get( iter_type beg, iter_type end, bool intl, std::ios_base& str,

               std::ios_base::iostate& err, long double& units ) const;
(1)
iter_type get( iter_type beg, iter_type end, bool intl, std::ios_base& str,
               std::ios_base::iostate& err, string_type& digits ) const;
(2)
protected:

virtual iter_type do_get( iter_type beg, iter_type end, bool intl, std::ios_base& str,

                          std::ios_base::iostate& err, long double& units ) const;
(3)
virtual iter_type do_get( iter_type beg, iter_type end, bool intl, std::ios_base& str,
                          std::ios_base::iostate& err, string_type& digits ) const;
(4)

从输入迭代器解析货币值并将结果写入 long double 或字符串。

1,2) 公有成员函数,调用最派生类的成员函数 do_get
3,4) 从输入迭代器 beg 读取字符,期望找到一个按照由 str.getloc() 中包含的 std::ctype facet(本页面其余部分称作 ct)、str.getloc() 中包含的 std::moneypunct<CharT, intl> facet(本页面其余部分称作 mp)以及从 str.flags() 获取的流格式化标志所指定规则格式化的货币值。

如果输入迭代器 beg 在解析完成前变得等于 end,则在 err 中设置 failbiteofbit。如果解析因其他原因失败,则在 err 中设置 failbit。无论哪种情况,在错误发生时都不会修改输出参数(unitsdigits)。

如果解析成功,则不改变 err,并将结果存储在 unitsdigits 中。

此函数使用的格式化 pattern 始终是 mp.neg_format()

如果 mp.grouping() 不允许千位分隔符,则遇到的第一个分隔符被视为解析错误,否则它们被视为可选。

如果 money_base::spacemoney_base::nonepattern 中的最后一个元素,则解析器在解析完货币值的其他组件后不会尝试消耗任何空白。否则,在 money_base::space 出现的位置会消耗一个或多个空白字符。

如果在 str.flags() 中设置了 showbase 标志,则需要货币符号或货币字符串;如果未设置,则货币符号是可选的。

如果 mp.positive_sign()mp.negative_sign() 返回的字符串的第一个字符在格式化模式的 money_base::sign 位置找到,则它被消耗,并且该字符串的其余字符在货币值的所有其他组件之后被期望和消耗。如果 mp.positive_sign()mp.negative_sign() 都非空,则符号是必需的,并且必须与其中一个字符串的第一个字符匹配。如果其中一个字符串为空,则符号是可选的(如果它不存在,则结果的符号对应于为空的字符串)。如果两个字符串都为空,或具有相同的第一个字符,则结果被赋予正号。如果输出参数是字符串(digits)并且结果为负,则值 ct.widen('-') 作为结果的第一个字符存储。

从输入中提取的数字按照它们出现的顺序放置在 digits 中(必要时通过 ct.widen() 进行加宽),或放入临时缓冲区 buf1,从该缓冲区构建 units 的值,如同通过

static const char src[] = "0123456789-";
CharT atoms[sizeof(src)];
ct.widen(src, src + sizeof(src) - 1, atoms);
for (int i = 0; i < n; ++i)
buf2[i] = src[find(atoms, atoms+sizeof(src), buf1[i]) - atoms];
buf2[n] = 0;
sscanf(buf2, "%Lf", &units);

(其中 n 是从输入中提取并存储在 buf1 中的字符数,buf2 是另一个足够大的字符缓冲区)。

目录

[edit] 返回值

一个迭代器,指向识别为货币字符串输入的有效部分之后的最后一个字符。

[edit] 注意

货币单位假定为货币的最小非分数单位:美国是美分,日本是日元。因此,在美国 locale 中,输入序列 "$1,056.23"units 中产生数字 105623.0,或在 digits 中产生字符串 "105623"

因为如果 showbase 关闭,货币符号是可选的,但整个多字符 negative_sign() 是必需的,所以给定格式化模式 {sign, value, space, symbol},并且 showbase 关闭,negative_sign"-",字符串 "-1.23 €" 解析为 -123 并在输入流上留下未消耗的“€”,但如果 negative_sign"()",则字符串 "(1.23 €)" 将被完全消耗。

I/O 操作符 std::get_money 为此函数提供了一个更简单的接口。

[edit] 示例

#include <iostream>
#include <locale>
#include <sstream>
 
void demo_money_get(std::locale loc, const std::string& input)
{
    std::istringstream str(input);
    str.imbue(loc);
    long double units;
 
    // The following can be written simpler with std::get_money(units)
    std::ios_base::iostate err = std::ios_base::goodbit;
    std::istreambuf_iterator<char> ret =
        std::use_facet<std::money_get<char>>(loc).get(
            std::istreambuf_iterator<char>(str),
            std::istreambuf_iterator<char>(),
            false, str, err, units);
    str.setstate(err);
    std::istreambuf_iterator<char> last{};
    if (str)
    {
        std::cout << "Successfully parsed '" << str.str() << "' as "
                  << units / 100 << " units\n";
        if (ret != last)
        {
            std::cout << "Remaining content: '";
            std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
            std::cout << "'\n";
        }
        else
            std::cout << "The input was fully consumed\n";
    }
    else
    {
        std::cout << "Parse failed. Unparsed string: '";
        std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
        std::cout << "'\n";
    }
}
 
int main()
{
    demo_money_get(std::locale("en_US.utf8"), "-$5.12 abc");
    demo_money_get(std::locale("ms_MY.utf8"), "(RM5.12) def");
}

输出

Successfully parsed '-$5.12 abc' as -5.12 units
Remaining content: ' abc'
Successfully parsed '(RM5.12) def' as -5.12 units
Remaining content: ' def'

[edit] 参阅

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