天天看點

C++基礎 - IO格式化

  标準庫定義了一組操縱符來修改流的格式狀态,一個操縱符是一個函數或是一個對象,會影響流的狀态,當操縱符改變流的格式狀态時,通常改變後的狀态對所有後續IO都生效。也有例外,比如endl。

控制布爾值格式

// 控制布爾值格式
    bool truev = true;
    bool falsev = false;
    cout << truev << " , " << falsev << endl;
    cout << boolalpha << truev << " , " << falsev << endl;
    cout << noboolalpha << truev << " , " << falsev << endl;
           
C++基礎 - IO格式化

指定整形值的進制,浮點數表示不受影響

cout << "default: " << 20 << " , " << 1024 << endl;
    cout << "oct: " << oct << 20 << " , " << 1024 << endl; // 八進制
    cout << "hex: " << hex << 20 << " , " << 1024 << endl; // 十六進制
    cout << "dec: " << dec << 20 << " , " << 1024 << endl << endl; // 十進制
           
C++基礎 - IO格式化

在輸出中指出進制,字元以大寫的形式列印

cout << showbase << uppercase;
    cout << "default: " << 20 << " , " << 1024 << endl;
    cout << "oct: " << oct << 20 << " , " << 1024 << endl; // 八進制
    cout << "hex: " << hex << 20 << " , " << 1024 << endl; // 十六進制
    cout << "dec: " << dec << 20 << " , " << 1024 << endl; // 十進制
    cout << noshowbase << nouppercase << endl;
           
C++基礎 - IO格式化

控制浮點數格式

1)指定列印精度(多少個數字)
           
cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl;
    cout.precision(12); // 成員函數precision, 有參版本,設定精度,無參版本,傳回目前精度
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl;
    cout << setprecision(3); // 操縱符
    cout << "Precision: " << cout.precision() << ", value: " << sqrt(2.0) << endl;
    cout << setprecision(6) << endl; // 恢複預設6位精度
           
C++基礎 - IO格式化
2)指定浮點數計數法
           
cout << "default format: " << 100 * sqrt(2.0) << endl; // 預設模式,精度控制數字的總位數
    cout << "scientific: " << scientific << 100 * sqrt(2.0) << endl; // 科學計數法
    cout << "fixed decimal: " << fixed << 100 * sqrt(2.0) << endl; // 定點十進制
    cout << "hexadecimal: " << hexfloat << 100 * sqrt(2.0) << endl; // 十六進制
    cout << "defaultfloat: " << defaultfloat << 100 * sqrt(2.0) << endl << endl; // 恢複預設模式
           
C++基礎 - IO格式化
3)列印小數點
           
cout << 10.0 << endl; // 預設,當一個浮點數的小數部分為0時,不顯示小數點,showpoint強制列印小數點
    cout << showpoint << 10.0 << endl;
    cout << noshowpoint << 10.0 << endl << endl;
           
C++基礎 - IO格式化

輸出補白

int i = -16;
    double d = 3.14159;
    // 預設右對齊
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 左對齊
    cout << left;
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 右對齊
    cout << right;
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 中間,控制負号的位置
    cout << internal;
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;

    // 設定補白字元
    cout << setfill('#');
    cout << "i: " << setw(12) << i << "next col" << endl;
    cout << "d: " << setw(12) << d << "next col" << endl;
           
C++基礎 - IO格式化

控制輸入格式

cin >> noskipws; // 設定輸入運算符讀取空白符(空格符, 制表符, 換行符,回車符)
    cin >> skipws; // 恢複到預設狀态
           

參考

C++ primer中文版 第五版 第17章

繼續閱讀