#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
/*1. 常用流狀態
showpos 在正數(包括0)之前顯示+號
showbase 十六進制整數前加0X,八進制整數前加0
uppercase 十六進制格式字母用大寫字母表示(默認爲小寫字母)
showpoint 浮點輸出即使小數點後都爲0也加小數點
boolalpha 邏輯值1和0用true和false表示
left 左對齊(填充字符填在右邊)
right 右對齊(填充字符填在左邊)
dec 十進制顯示整數
hex 十六進制顯示整數
oct 八進制顯示整數
fixed 定點數格式輸出
scientific 科學計數法格式輸出
取消流狀態的操作爲:
noshowpos,noshowbase,nouppercase,noshowpoint,noboolalpha
*/
cout << showpos << 12 << endl;
cout << hex << 18 << " " << showbase << 18 << endl;
cout << hex << 255 << " " << uppercase << 255 << endl;
cout << 123.0 << " " << showpoint << 123.0 << endl;
cout << (2 > 3) << " " << boolalpha << (2 > 3) << endl;
cout << fixed << 12345.678 << endl;
cout << scientific << 12345.678 << endl;
cout.unsetf(ios::scientific);
/*
有參數的三個常用的流狀態
width(int) //設置顯示寬度
fill(int) //設置填充字符
precision(int) //設置有效位數(普通顯示方式)或精度(定點或科學計數法方式)
這些流狀態是以cout捆綁調用它們的形式設置的,不能與流出符<<連用。
特別注意width(n)爲一次性操作,即第二次顯示時將不再有效。
默認爲width(0),表示僅顯示數值。例如如下例子
*/
cout.width(5);
cout.fill('S');
cout << dec << noshowpos << 23 << 23 << endl; //輸出 :SSS2323
/*
3. 與 << 連用的設置方式
還有另一種與流出符 << 連用的設置方式,但在使用時,要包含另一個頭文件 iomanip
setw(int)
setfill(char)
setprecision(int)
如下例子
*/
cout << setw(6) << setfill('$') << 27 << endl; // 輸出 $$$$27
cout << setfill('M') << setw(10) << 'M' << endl;//輸出10個M
system("pause");
return 0;
}
/*第1部分輸出
+12
12 0x12
0xff 0XFF
+123 +123.000
0 false
+12345.678000
+1.234568E+004
請按任意鍵繼續. . .
*/
/*
left 和 right 是對立的,設置了此,就取消了彼。dec/oct/hex三者也是互相獨立的,設置了此,取消了彼
fixed與scientific和一般顯示方式三者也是獨立的,不過它們的取消方式比較不同,
爲cout捆綁函數調用的方式:
cout.unsetf(ios::scientific);
*/