名前空間
変種

std::num_put

提供: cppreference.com
 
 
 
 
<tbody> </tbody>
ヘッダ <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, double, long double, void* およびこれらに暗黙に変換可能なすべての型 (intfloat など) がサポートされます。 標準の書式付き出力操作 (cout << n; など) は数値のテキスト表現を生成するために入出力ストリームのロケールの std::num_put ファセットを使用します。

cpp/locale/locale/facet

継承図

型の要件

-
OutputItLegacyOutputIterator の要件を満たさなければなりません。

特殊化

2つのスタンドアロンな (ロケール非依存の) 完全特殊化および2つの部分特殊化が標準ライブラリによって提供されます。

ヘッダ <locale> で定義
std::num_put<char> 数値のナロー文字列表現を作成します
std::num_put<wchar_t> 数値のワイド文字列表現を作成します
std::num_put<char, OutputIt> カスタム出力イテレータを用いて数値のナロー文字列表現を作成します
std::num_put<wchar_t, OutputIt> カスタム出力イテレータを用いて数値のワイド文字列表現を作成します

さらに、 C++ のプログラム内で構築したすべてのロケールオブジェクトは、これらの特殊化の独自の (ロケール固有の) バージョンを実装します。

メンバ型

メンバ型 定義
char_type CharT
iter_type OutputIt

メンバ関数

新しい num_put ファセットを構築します
(パブリックメンバ関数)
num_put ファセットを破棄します
(プロテクテッドメンバ関数)
do_put を呼びます
(パブリックメンバ関数)

プロテクテッドメンバ関数

[仮想]
数値を書式化して出力ストリームに書き込みます
(仮想プロテクテッドメンバ関数)

メンバオブジェクト

static std::locale::id id
ロケールの id
(パブリックメンバオブジェクト)

#include <iostream>
#include <locale>
#include <string>
#include <iterator>

int main()
{
    double n = 1234567.89;
    std::cout.imbue(std::locale("de_DE"));
    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

関連項目

数値の区切り文字の規則を定義します
(クラステンプレート) [edit]
入力文字シーケンスから数値をパースします
(クラステンプレート) [edit]
(C++11)
整数または浮動小数点値を string に変換します
(関数) [edit]
整数または浮動小数点値を wstring に変換します
(関数) [edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.