long setf(long <フラッグ>); skipws = 0x0001 : ストリームの入力中,先頭の空白文字を読み飛ばす left = 0x0002 : 出力を左寄せ right = 0x0004 : 出力を右寄せ internal = 0x0008 : 符号と数字の間にブランクを入れ出力幅一杯にする dec = 0x0010 : 10進表記(デフォルト) oct = 0x0020 : 8進表記 hex = 0x0040 : 16進表記 showbase = 0x0080 : 数値の基数の表示 showpoint = 0x0100 : 浮動小数点の出力で小数点,右端の0,eを表示 uppercase = 0x0200 : 浮動小数点のe等を大文字で表示 showpos = 0x0400 : 正の数値の前に+を表示 scientific = 0x0800 : 実数値を浮動小数点表示 fixed = 0x1000 : 固定小数点表示(デフォルト) unitbuf = 0x2000 : 出力の度に,すべてのストリームをフラッシュ stdio = 0x4000 : 出力の度に,stdout及びstderrをフラッシュ
stream.setf(ios::showbase);
cout.setf(ios::scientific | ios::showpos);
long unsetf(long <フラッグ>); // フラッグをoff long flags(); // フラッグの現在値を返す long flags(long <フラッグ>); // フラッグをonにし,直前の値を返す
int width(int <長さ>); // 出力幅制御 char fill(char <文字>); // 空白を埋める文字 int precision(int <桁数>); // 小数点以下の桁数 //(デフォルトは 6 桁.固定小数点の場合は, // 小数点や符号を除いた全体の桁数になり, // 表示できないときは浮動小数点表示となる)
/********************************/
/* 書式付き出力(メンバー関数) */
/* coded by Y.Suganuma */
/********************************/
#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::showpos); // +の表示
cout.setf(ios::scientific); // 浮動小数点表示
cout << 123.45 << " " << 3.141592654 << "\n";
cout.precision(4); // 小数点以下4桁
cout.width(10); // 出力幅10桁
cout << 123.45 << " " << 3.141592654 << "\n";
cout.fill('*'); // *で埋める
cout.width(15); // 出力幅15桁
cout << 123.45 << " " << 3.141592654 << "\n";
return 0;
}
+1.234500e+002 +3.141593e+000 +1.2345e+002 +3.1416e+000 ***+1.2345e+002 +3.1416e+000
dec : 10 進表記
endl : 改行文字の出力
ends : 空白文字の出力
flush : ストリームのフラッシュ
hex : 16 進表記
oct : 8 進表記
resetioflags(long f) : f で指定されたフラッグを off
setbase(int base) : 基数を base にする
setfill(int ch) : 文字 h で埋める
setioflags(long f) : f で指定されたフラッグを on
setprecision(int p) : 小数点以下 p 桁(デフォルトは 6 桁.固定小数点の場合は,
小数点や符号を除いた全体の桁数になり,
表示できないときは浮動小数点表示となる)
setw(int w) : 出力幅を w
ws : 先頭の空白を読み飛ばす
/******************************/
/* 書式付き出力(書式操作子) */
/* coded by Y.Suganuma */
/******************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setiosflags(ios::showpos);
cout << setiosflags(ios::scientific);
cout << 123.45 << " " << 3.141592654 << endl;
// 次のように,命令と出力値を一緒に書いても構わない
cout << setprecision(4) << setw(10) << 123.45 << " " << 3.141592654 << endl;
cout << setfill('*');
cout << setw(15);
cout << 123.45 << " " << 3.141592654 << endl;
return 0;
}
+1.234500e+002 +3.141593e+000 +1.2345e+002 +3.1416e+000 ***+1.2345e+002 +3.1416e+000