std::setbase
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <iomanip> で定義
|
||
/*unspecified*/ setbase( int base ); |
||
ストリームの基数を設定します。 式 out << setbase(base) または in >> setbase(base) で使用されたとき、ストリーム out または in の basefield フラグを base の値に応じて変更します。
- 値
16はbasefieldを std::ios_base::hex に設定します。 - 値
8はbasefieldを std::ios_base::oct に設定します。 - 値
10はbasefieldを std::ios_base::dec に設定します。
8, 10, 16 以外の base の値は basefield をゼロにリセットします。 これは10進数の出力および接頭辞依存の入力に対応します。
引数
| base | - | basefield の新しい値 |
戻り値
str が std::basic_ostream<CharT, Traits> または std::basic_istream<CharT, Traits> 型の出力ストリームの名前である場合に式 str << setbase(base) または str >> setbase(base) が以下のコードが実行されたかのように動作するような、未規定な型のオブジェクトを返します。
str.setf(base == 8 ? std::ios_base::oct :
base == 10 ? std::ios_base::dec :
base == 16 ? std::ios_base::hex :
std::ios_base::fmtflags(0),
std::ios_base::basefield);
例
Run this code
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::cout << "Parsing string \"10 0x10 010\"\n";
int n1, n2, n3;
std::istringstream s("10 0x10 010");
s >> std::setbase(16) >> n1 >> n2 >> n3;
std::cout << "hexadecimal parse: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
s.clear();
s.seekg(0);
s >> std::setbase(0) >> n1 >> n2 >> n3;
std::cout << "prefix-dependent parse: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
std::cout << "hex output: " << std::setbase(16)
<< std::showbase << n1 << ' ' << n2 << ' ' << n3 << '\n';
}
出力:
Parsing string "10 0x10 010"
hexadecimal parse: 16 16 16
prefix-dependent parse: 10 16 8
hex output: 0xa 0x10 0x8
関連項目
| 整数の入出力に使用される基数を変更します (関数) | |
| 数値の基数を表すために接頭辞を使用するかどうか制御します (関数) |