iomanip是C++下控制I/O流的一个标准库,利用它,可以实现格式化输出。
不同进制输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | #include <iostream> #include <iomanip> using namespace std;
  int main() {     int a = 123;
           cout << showbase << setbase(8) << a << endl;     cout << oct << a << endl;
           cout << showbase << setbase(10) << a << endl;     cout << dec << a << endl;
           cout << showbase << setbase(16) << a << endl;     cout << hex << a << endl; }
   | 
 
保留有效数字
1 2 3 4 5 6 7 8 9 10
   | #include <iostream> #include <iomanip> using namespace std;
  int main() {     double a = 1.2;
           cout << setprecision(3) << a << endl; }
   | 
 
保留小数
1 2 3 4 5 6 7 8 9 10
   | #include <iostream> #include <iomanip> using namespace std;
  int main() {     double a = 1.2;
           cout << fixed << setprecision(3) << a << endl; }
   | 
 
输出前导0
1 2 3 4 5 6 7 8 9 10
   | #include <iostream> #include <iomanip> using namespace std;
  int main() {     int a = 123;
           cout << setfill('0') << setw(4) << a << endl; }
   |