setw(n)是c++中在输出操作中使用的字段宽度设置,n表示字段宽度。
用该函数时必须用头函数名声明:#include<iomanip>进行声明
效果图如下:代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout<< setw(4) <<123 <<1234<<endl;
cout << 1 <<setw(7) << 1234 << endl;
cout << 1 << setw(7) << 1234567891234 << endl;
cout << 1 << 1234 << setw(7) << endl;
}
运行结果:
1231234
1 123411234567891234
11234
请按任意键继续. . .