Namespaces
Variants

std::money_put<CharT,OutputIt>:: put, do_put

From cppreference.net
std::money_put
Member functions
money_put::put money_put::do_put
定义于头文件 <locale>
public :

iter_type put ( iter_type out, bool intl, std:: ios_base & f,

char_type fill, long double quant ) const ;
(1)
iter_type put ( iter_type out, bool intl, std:: ios_base & f,
char_type fill, const string_type & quant ) const ;
(2)
protected :

virtual iter_type do_put ( iter_type out, bool intl, std:: ios_base & str,

char_type fill, long double units ) const ;
(3)
virtual iter_type do_put ( iter_type out, bool intl, std:: ios_base & str,
char_type fill, const string_type & digits ) const ;
(4)

格式化货币值并将结果写入输出流。

1,2) 公开成员函数,调用最终派生类的成员函数 do_put
3) 数值参数 units 被转换为宽字符字符串,转换方式类似于 ct. widen ( buf1, buf1 + std:: sprintf ( buf1, "%.0Lf" , units ) , buf2 ) ,其中 ct str. getloc ( ) 中植入的 std::ctype 平面, buf1 buf2 是足够大的字符缓冲区。生成的字符字符串 buf2 将按照下述方式进行处理、格式化并输出到 out
4) 从字符串参数 digits 中,仅提取可选的起始负号(通过比较 ct. widen ( '-' ) 确定,其中 ct str. getloc ( ) 中注入的 std::ctype 平面)以及紧随其后的数字字符(由 ct 分类),将这些字符序列作为待处理内容,按照下述方式格式化并输出到 out

给定前序步骤中的字符序列,若首字符等于 ct. widen ( '-' ) ,则调用 mp. neg_format ( ) 获取负值格式化 模式 ;否则调用 mp. pos_format ( ) ,其中 mp 为注入 str. getloc ( ) std:: moneypunct < CharT, intl > 区域策略facet。

千位分隔符和小数点字符根据 mp. grouping ( ) mp. frac_digits ( ) mp. decimal_point ( ) mp. thousands_sep ( ) 的要求插入,生成的字符串被放置在输出序列中格式化模式里 value 出现的位置。

如果 str. flags ( ) & str. showbase 非零(即使用了 std::showbase 操纵符),则通过调用 mp. curr_symbol ( ) 生成货币符号或字符串,并将其置于格式化模式中 symbol 出现位置的输出序列中。

如果 mp. positive_sign ( ) (当使用正数格式模式时)或 mp. negative_sign ( ) (当使用负数格式模式时)返回的字符串包含多个字符,则返回的首字符会被置于格式化模式中 sign 出现的位置,其余字符则被放置在所有其他字符之后。例如,使用格式化模式 { sign, value, space, symbol } 处理数值 123 时:若 negative_sign 为 "-" 可能生成 "-1.23 €" ,而 negative_sign 为 "()" 则会生成 "(1.23 €)"

如果为指定格式生成的字符数少于 str. width ( ) 返回的值,则会插入 fill 的副本,使输出序列的总长度恰好达到 str. width ( ) ,具体方式如下:

  • 如果 str. flags ( ) & str. adjustfield 等于 str. internal ,填充字符将被插入到格式化模式中 none space 出现的位置。
  • 否则,如果 str. flags ( ) & str. adjustfield 等于 str. left ,则 fill 的副本将附加在所有其他字符之后。
  • 否则,填充字符将被放置在所有其他字符之前。

最后调用 str. width ( 0 ) 来取消任何 std::setw 的效果。

目录

返回值

指向最后生成字符之后位置的迭代器。

注释

货币单位假定为该货币的最小非分数单位:在美国为美分,在日本为日元。

示例

#include <iomanip>
#include <iostream>
#include <locale>
struct my_punct : std::moneypunct_byname<char, false>
{
    my_punct(const char* name) : moneypunct_byname(name) {}
    string_type do_negative_sign() const { return "()"; }
};
int main()
{
    std::locale loc("ru_RU.utf8");
    std::cout.imbue(loc);
    long double units = -123.45;
    std::cout << "In Russian locale, " << units << " prints as " << std::showbase;
    // 注意:以下代码等效于直接调用 std::put_money(units)
    std::use_facet<std::money_put<char>>(loc).put(
        {std::cout}, false, std::cout, std::cout.fill(), units);
    std::cout << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new my_punct("ru_RU.utf8")));
    std::cout << "With negative_sign set to \"()\", it prints as ";
    std::use_facet<std::money_put<char>>(loc).put(
        {std::cout}, false, std::cout, std::cout.fill(), units);
    std::cout << '\n';
}

输出:

In Russian locale, -123,45 prints as -1.23 руб
With negative_sign set to "()", it prints as (1.23 руб)

缺陷报告

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

缺陷报告 应用于 发布时行为 正确行为
LWG 328 C++98 用于 std::sprintf 的格式字符串为 "%.01f" 修正为 "%.0Lf"

参见

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