最終更新日時:
が更新

履歴 編集

class template
<locale>

std::num_put

namespace std {
  template <class charT, class OutputIterator = ostreambuf_iterator<charT> >
  class num_put : public locale::facet;
}

概要

num_putは、数値・真偽値・ポインタを書式化して出力ストリームへ出力するためのロケールファセットである。basic_ostreamの数値出力演算子operator<<は、このファセットを介して出力の書式化を行う。

テンプレートパラメータOutputIteratorは、出力に使用するイテレータの型を表し、既定ではstd::ostreambuf_iterator<charT>である。

書式化処理はprotectedな仮想関数do_putに実装されており、publicメンバ関数putから呼び出される。

メンバ関数

publicメンバ関数

名前 説明
(constructor) コンストラクタ
put 数値を出力する

静的メンバ変数

名前 説明
static locale::id id; このファセットを識別するためのID

protectedメンバ関数

名前 説明
(destructor) デストラクタ
do_put 数値を出力する (virtual)

浮動小数点数の書式変換

do_putが浮動小数点数を出力するとき、ios_basefloatfielduppercaseの状態に応じて、以下のprintf変換指定子に相当する書式が使われる。

状態 相当するprintf変換
floatfield == ios_base::fixed && !uppercase %f
floatfield == ios_base::fixed %F
floatfield == ios_base::scientific && !uppercase %e
floatfield == ios_base::scientific %E
floatfield == (ios_base::fixed \| ios_base::scientific) && !uppercase %a
floatfield == (ios_base::fixed \| ios_base::scientific) %A
!uppercase(上記以外) %g
(それ以外) %G

メンバ型

名前 説明
char_type 文字型 charT
iter_type 出力のイテレータ型 OutputIterator

#include <iostream>
#include <sstream>
#include <locale>
#include <iterator>

int main()
{
  std::ostringstream oss;

  // ストリームのロケールからnum_putファセットを取得する
  const auto& facet = std::use_facet<std::num_put<char>>(oss.getloc());

  // 幅8で右詰め、埋め文字は'*'
  oss.width(8);
  facet.put(std::ostreambuf_iterator<char>{oss}, oss, '*', 42L);

  std::cout << oss.str() << std::endl;
}

出力

******42

バージョン

言語

  • C++98

関連項目

参照

  • LWG Issue 4084. std::fixed ignores std::uppercase
    • C++26で、floatfield == ios_base::fixedのときにuppercaseが設定されていれば%F(大文字)が使われることが明確化された(従来はfixedが常に%fuppercaseが無視されていた)