名前空間
変種
操作

std::num_put

From cppreference.com
< cpp‎ | locale
 
 
 
 
 
ヘッダー <locale> で定義
template<

    class CharT,
    class OutputIt = std::ostreambuf_iterator<CharT>

> class num_put;

クラス std::num_put は、数値を文字列としてフォーマットする規則をカプセル化します。具体的には、型 bool, long, unsigned long, long long, unsigned long long(since C++11), double, long double, void*、およびこれらに暗黙的に変換可能なすべての型(intfloat など)がサポートされています。標準のフォーマット出力演算子(cout << n; など)は、I/O ストリームのロケールの std::num_put ファセットを使用して、数値のテキスト表現を生成します。

cpp/locale/locale/facetstd-num put-inheritance.svg

継承図

std::num_put の特殊化が標準ライブラリによって提供されることが保証されていない場合(下記参照)、その put() および do_put() の動作は、指定されたとおりには保証されません。

目次

[編集] 特殊化

標準ライブラリは、以下の特殊化を提供することが保証されています(これらは あらゆるロケールオブジェクトによって実装される必要があります)。

ヘッダー <locale> で定義
std::num_put<char> 数値を狭い文字列表現で生成します。
std::num_put<wchar_t> 数値をワイド文字列表現で生成します。

さらに、標準ライブラリは、以下の型要件を満たすすべての特殊化を提供することも保証されています。

[編集] ネストされた型

定義
char_type CharT
iter_type OutputIt

[編集] データメンバ

メンバ 説明
std::locale::id id [static] ファセットの識別子

[編集] メンバ関数

新しい num_put ファセットを構築します。
(public member function)
do_put を呼び出します。
(public member function)

[編集] protected メンバ関数

num_put ファセットを破棄します。
(protected メンバ関数)
[virtual]
数値をフォーマットし、出力ストリームに書き込みます。
(仮想 protected メンバ関数)

[編集]

#include <iostream>
#include <iterator>
#include <locale>
#include <string>
 
int main()
{
    double n = 1234567.89;
    std::cout.imbue(std::locale("de_DE.UTF-8"));
    std::cout << "Direct conversion to string:\n"
              << std::to_string(n) << '\n'
              << "Output using a german locale:\n"
              << std::fixed << n << '\n'
              << "Output using an american locale:\n";
 
    // use the facet directly
    std::cout.imbue(std::locale("en_US.UTF-8"));
    auto& f = std::use_facet<std::num_put<char>>(std::cout.getloc());
    f.put(std::ostreambuf_iterator<char>(std::cout), std::cout, ' ', n);
    std::cout << '\n';
}

実行結果の例

Direct conversion to string:
1234567.890000
Output using a german locale:
1.234.567,890000
Output using an american locale:
1,234,567.890000

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 427 C++98 num_put は、以下の条件を満たす任意の CharT を受け入れることが保証されていました。
iostream コンポーネント をインスタンス化できる
文字の要件を満たす
char
wchar_t、およびその他の実装定義
の文字型のみを受け入れることが保証されています。
LWG 2392 C++98 num_put が受け入れることが保証できるのは、
文字型 CharT のみでした。
実装定義の
文字コンテナ型を受け入れることを保証できます。

[編集] 関連項目

数値の句読点ルールを定義する
(クラステンプレート) [編集]
入力文字列シーケンスから数値を解析する
(クラステンプレート) [編集]
(C++11)
整数値または浮動小数点値を string に変換する
(function) [編集]
整数値または浮動小数点値を wstring に変換する
(function) [編集]
English 日本語 中文(简体) 中文(繁體)