// This program illustrates the use of the left and right manipulators. #include <iostream> #include <iomanip> // Header file needed to use stream manipulators #include <string> // Header file needed to use string objects using namespace std; int main() { string month1 = "January", month2 = "February", month3 = "March"; int days1 = 31, days2 = 28, days3 = 31; double high1 = 22.6, high2 = 37.4, high3 = 53.9; cout << fixed << showpoint << setprecision(1); cout <<"Month Days Highn"; cout << left << setw(12) << month1 << right << setw(4) << days1 << setw(9) << high1 << endl; cout << left << setw(12) << month1 << right << setw(4) << days1 << setw(9) << high1 << endl; cout << left << setw(12) << month1 << right << setw(4) << days1 << setw(9) << high1 << endl; return 0; }程式輸出結果:
Month Days High
January 31 22.6
January 31 22.6
January 31 22.6
流操作符 | 描 述 |
---|---|
setw(n) | 為下一個值的輸出設定最小列印欄位寬度為 n |
fixed | 以固定點(例如小數點)的形式顯示浮點數 |
showpoint | 顯示浮點數的小數點和尾數 0,即使沒有小數部分也一樣 |
setprecision(n) | 設定浮點數的精度 |
left | 使後續輸出左對齊 |
right | 使後續輸出右對齊 |