如以下代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x = 24; double y = 12.3456789;
cout << dec << x << " ";
cout << hex << x << " ";
cout << oct << x << '\n';
cout << dec;
cout << setw(8) << x << "," << x << endl;
cout << setw(8) << setfill('*') << x << endl;
cout << setprecision(5) << y << endl;
return 0;
}
该代码对以上控制符都进行了展示。运行结果如下图:
setw是设置数字对齐的最佳方式,并且可以附加setfill进行符号填充。