名前空間
変種
操作

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

From cppreference.com
< cpp‎ | locale‎ | money put
 
 
 
 
std::money_put
メンバ関数
money_put::putmoney_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) public メンバ関数は、最も派生したクラスのメンバ関数 do_put を呼び出します。
3) 数値引数 units は、ctstr.getloc() に埋め込まれた std::ctype ファセットであり、buf1 および buf2 が十分に大きな文字バッファである場合、ct.widen(buf1, buf1 + std::sprintf(buf1, "%.0Lf", units), buf2) でのように、ct.widen(buf1, buf1 + std::sprintf(buf1, "%.0Lf", units), buf2) としてワイド文字文字列に変換されます。結果の文字列 buf2 は、以下に説明するように処理、フォーマットされ、out に出力されます。
4) 文字列引数 digits から、オプションの先頭のマイナス記号(ctstr.getloc() に埋め込まれた std::ctype ファセットである場合、ct.widen('-') との比較によって決定される)と、それに続く数字文字(ct によって分類される)のみが、以下に説明するように処理、フォーマットされ、out に出力される文字シーケンスとして取得されます。

前のステップからの文字シーケンスが与えられた場合、最初の文字が ct.widen('-') と等しい場合、フォーマット pattern を取得するために mp.neg_format() が呼び出され、それ以外の場合は mp.pos_format() が呼び出されます。ここで、mpstr.getloc() に埋め込まれた std::moneypunct<CharT, intl> ファセットです。

千単位の区切り文字と小数点文字は、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()(負のフォーマットパターンの場合)が1文字より長い文字列を返す場合、最初に返された文字はフォーマットパターンで sign が現れる位置に出力シーケンスに配置され、残りの文字は他のすべての文字の後ろに配置されます。例えば、単位が 123 で negative_sign が "-" の場合、フォーマットパターン {sign, value, space, symbol}"-1.23 €" になる可能性がありますが、negative_sign が "()" の場合は "(1.23 €)" が生成されます。

指定されたフォーマットのために生成された文字数が str.width() によって返される値より小さい場合、出力シーケンスの総長がちょうど str.width() になるように、fill のコピーが挿入されます。挿入方法は以下の通りです。

  • もし str.flags() & str.adjustfieldstr.internal と等しい場合、フィル文字はフォーマットパターンで none または space が現れる場所に挿入されます。
  • そうでなければ、もし str.flags() & str.adjustfieldstr.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;
 
    // note, the following is equivalent to simply 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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 328 C++98 "%.01f" だった std::sprintf のためのフォーマット文字列 "%.0Lf" に修正

[編集] 関連項目

std::money_getstd::money_put で使用される通貨書式設定パラメータを定義する
(クラステンプレート) [編集]
入力文字列シーケンスから通貨値を解析・構築する
(クラステンプレート) [編集]
(C++11)
通貨の値をフォーマットして出力する
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)