C++ 標準ライブラリ <iomanip>
<iomanip>これはC++標準ライブラリのヘッダーファイルであり、入出力ストリームの書式設定操作を提供します。
iomanipライブラリ内の関数により、開発者は小数点以下の桁数、幅、配置などの出力形式を制御できます。
iomanipiomanip は Input/Output Manipulators の略であり、C++ 標準ライブラリの入出力ストリームの形式を制御するための一連のマニピュレータを提供します。以下のシナリオに適用されます:
- 科学技術計算における浮動小数点数の形式の処理;
- データの整列と整形;
- 特定の基数または形式で数値を表示します。
構文
iomanipライブラリ内の関数は通常、<<と>>マニピュレータと共に使用され、出力ストリームの制御を実現します。
以下は一般的に使用されるiomanip関数:
| 関数/マニピュレータ | 機能 | サンプルコード | 出力結果 |
|---|---|---|---|
std::setw(int n) | フィールド幅を設定し、次の出力に幅を指定する | std::cout << std::setw(5) << 42; | 42 |
std::setfill(char) | 埋め文字を設定する(デフォルトはスペース) | std::cout << std::setfill('*') << std::setw(5) << 42; | ***42 |
std::left | 左揃えを設定する | std::cout << std::left << std::setw(5) << 42; | 42 |
std::right | 右揃えを設定する | std::cout << std::right << std::setw(5) << 42; | 42 |
std::internal | 符号を左に、残りを右に配置する | std::cout << std::internal << std::setw(5) << -42; | - 42 |
std::setprecision(int) | 浮動小数点数の有効桁数を設定する | std::cout << std::setprecision(3) << 3.14159; | 3.14 |
std::fixed | 固定小数点形式で浮動小数点数を出力する | std::cout << std::fixed << std::setprecision(2) << 3.14159; | 3.14 |
std::scientific | 科学表記法形式で浮動小数点数を出力する | std::cout << std::scientific << 3.14159; | 3.141590e+00 |
std::hex | 整数を16進数で表示する | std::cout << std::hex << 42; | 2a |
std::oct | 整数を8進数で表示する | std::cout << std::oct << 42; | 52 |
std::dec | 整数を10進数で表示する(デフォルト) | std::cout << std::dec << 42; | 42 |
std::showbase | 基数プレフィックスを表示する(例:0x16進数を示す) | std::cout << std::showbase << std::hex << 42; | 0x2a |
std::noshowbase | 基数プレフィックスを非表示にする(デフォルト) | std::cout << std::noshowbase << std::hex << 42; | 2a |
std::uppercase | 16進数の英字を大文字で表示する | std::cout << std::uppercase << std::hex << 42; | 2A |
std::nouppercase | 16進数の英字を小文字で表示する(デフォルト) | std::cout << std::nouppercase << std::hex << 42; | 2a |
std::showpos | 正の数の前に+符号を表示する | std::cout << std::showpos << 42; | +42 |
std::noshowpos | 正の数の+符号を表示しない(デフォルト) | std::cout << std::noshowpos << 42; | 42 |
std::boolalpha | ブール値をtrue/falseで出力する | std::cout << std::boolalpha << true; | true |
std::noboolalpha | ブール値を1/0で出力する(デフォルト) | std::cout << std::noboolalpha << true; | 1 |
std::setbase(int n) | 整数の基数を設定する(8、10、16に対応) | std::cout << std::setbase(16) << 42; | 2a |
std::resetiosflags | 指定したストリーム状態をリセットする | std::cout << std::resetiosflags(std::ios::showbase) << std::hex << 42; | 2a |
std::setiosflags | 指定したストリーム状態を設定する | std::cout << std::setiosflags(std::ios::showbase) << std::hex << 42; | 0x2a |
例
1. 幅の設定
を使用するとsetw出力の幅を設定できます。出力内容の文字数が設定した幅より少ない場合、残りの部分はスペースで埋められます。
例
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setw(10) << "Hello" << std::endl;
return 0;
}
#include <iomanip>
int main() {
std::cout << std::setw(10) << "Hello" << std::endl;
return 0;
}
出力結果:
Hello
2. 精度の設定
を使用するとsetprecision浮動小数点数の有効桁数を設定できます。
例
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.141592653589793;
std::cout << "Default: " << pi << "\n";
std::cout << "Set precision (3): " << std::setprecision(3) << pi << "\n";
std::cout << "Set precision (7): " << std::setprecision(7) << pi << "\n";
return 0;
}
#include <iomanip>
int main() {
double pi = 3.141592653589793;
std::cout << "Default: " << pi << "\n";
std::cout << "Set precision (3): " << std::setprecision(3) << pi << "\n";
std::cout << "Set precision (7): " << std::setprecision(7) << pi << "\n";
return 0;
}
出力結果:
Default: 3.14159 Set precision (3): 3.14 Set precision (7): 3.141593
3. 固定小数点と科学表記法
fixedとscientific浮動小数点数の出力形式を制御できます。
例
#include <iostream>
#include <iomanip>
int main() {
double num = 123456789.0;
std::cout << "Fixed: " << std::fixed << num << std::endl;
std::cout << "Scientific: " << std::scientific << num << std::endl;
return 0;
}
#include <iomanip>
int main() {
double num = 123456789.0;
std::cout << "Fixed: " << std::fixed << num << std::endl;
std::cout << "Scientific: " << std::scientific << num << std::endl;
return 0;
}
出力結果:
Fixed: 123456789.000000 Scientific: 1.23456789e+08
4. 埋め文字の設定
を使用するとsetfill埋め文字を設定できます。通常はsetwと一緒に使用します。
例
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setfill('*') << std::setw(10) << "World" << std::endl;
return 0;
}
#include <iomanip>
int main() {
std::cout << std::setfill('*') << std::setw(10) << "World" << std::endl;
return 0;
}
出力結果:
*****World
5. 書式フラグの設定とリセット
setiosflagsとresetiosflagsストリームの書式フラグを設定またはリセットできます。
例
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setiosflags(std::ios::uppercase) << std::hex << 255 << std::endl;
std::cout << std::resetiosflags(std::ios::uppercase) << std::hex << 255 << std::endl;
return 0;
}
#include <iomanip>
int main() {
std::cout << std::setiosflags(std::ios::uppercase) << std::hex << 255 << std::endl;
std::cout << std::resetiosflags(std::ios::uppercase) << std::hex << 255 << std::endl;
return 0;
}
出力結果:
FF ffその他の拡張