std::showbase, std::noshowbase
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <ios> で定義
|
||
std::ios_base& showbase( std::ios_base& str ); |
(1) | |
std::ios_base& noshowbase( std::ios_base& str ); |
(2) | |
1) str.setf(std::ios_base::showbase) を呼んだかのように、ストリーム str の showbase フラグを有効化します。
2) str.unsetf(std::ios_base::showbase) を呼んだかのように、ストリーム str の showbase フラグを無効化します。
これは入出力マニピュレータであるため、 std::basic_ostream 型の任意の out に対する out << std::showbase のような式や std::basic_istream 型の任意の in に対する in >> std::showbase のような式で呼ぶことができます。
showbase フラグは整数の出力 (std::num_put::put を参照)、金額の入力 (std::money_get::get を参照) および金額の出力 (std::money_put::put を参照) の動作に影響を与えます。
引数
| str | - | 入出力ストリームへの参照 |
戻り値
str (操作後のストリームへの参照)。
ノート
std::num_put::put で規定されているように、整数の出力における showbase フラグは std::printf の # 書式指定子のように振る舞います。 つまり、値ゼロを出力するときは基数の接頭辞を追加しないという意味です。
例
Run this code
#include <sstream>
#include <locale>
#include <iostream>
#include <iomanip>
int main()
{
// showbase affects the output of octals and hexadecimals
std::cout << std::hex
<< "showbase: " << std::showbase << 42 << '\n'
<< "noshowbase: " << std::noshowbase << 42 << '\n';
// and both input and output of monetary values
std::locale::global(std::locale("en_US.utf8"));
long double val = 0;
std::istringstream is("3.14");
is >> std::showbase >> std::get_money(val);
std::cout << "With showbase, parsing 3.14 as money gives " << val << '\n';
is.seekg(0);
is >> std::noshowbase >> std::get_money(val);
std::cout << "Without showbase, parsing 3.14 as money gives " << val << '\n';
}
出力:
showbase: 0x2a
noshowbase: 2a
With showbase, parsing 3.14 as money gives 0
Without showbase, parsing 3.14 as money gives 314
関連項目
| 指定された ios_base のフラグをクリアします (関数) | |
| 指定された ios_base のフラグを設定します (関数) |