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 std::ctype ファセットであり、 str. getloc ( ) にインプレースされています。また buf1 buf2 は十分な大きさの文字バッファです。生成された文字列 buf2 は、以下に説明するように処理、フォーマットされ、 out に出力されます。
4) 文字列引数 digits から、オプションの先頭マイナス記号( ct. widen ( '-' ) との比較によって決定される。ここで ct std::ctype ファセットで str. getloc ( ) にインプレースされている)と、その直後に続く数字文字( ct によって分類される)のみが、処理・フォーマットされ、以下に説明するように out に出力される文字シーケンスとして取り出される。

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

桁区切り文字と小数点文字は、 mp. grouping ( ) mp. frac_digits ( ) mp. decimal_point ( ) および mp. thousands_sep ( ) によって要求される通りに挿入され、結果の文字列は書式パターン内で value が現れる位置に出力シーケンスに配置されます。

str. flags ( ) & str. showbase が非ゼロの場合( std::showbase マニピュレータが使用された場合)、通貨記号または文字列は mp. curr_symbol ( ) を呼び出すことで生成され、フォーマットパターン内の symbol が現れる位置に出力シーケンスに配置されます。

positive format pattern(正のフォーマットパターンが使用される場合)で mp. positive_sign ( ) が、またはnegative format pattern(負のフォーマットパターンが使用される場合)で 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 の効果をキャンセルします。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - その他のテキスト(Return value、Notes、Example、Defect reports、See also)はC++関連の専門用語として翻訳せず、原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどはすべて変更せず保持 - 番号付けや構造も完全に維持

戻り値

生成された最後の文字の直後を指すイテレータ。

注記

通貨単位は、通貨の最小の非分数単位と見なされます:米国ではセント、日本では円。

#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 std::sprintf で使用されるフォーマット文字列は "%.01f" であった "%.0Lf" に修正された

関連項目

std::money_get および std::money_put で使用される通貨書式パラメータを定義する
(クラステンプレート)
入力文字シーケンスから通貨値を解析および構築する
(クラステンプレート)
(C++11)
通貨値を書式設定して出力する
(関数テンプレート)