setw(n)是c++中在输出操作中使用的字段宽度设置,n表示字段宽度。
用该函数时必须用头函数名声明:#include<iomanip>进行声明
n若超过下一段输出内容的长度,则在内容前用空格补齐,反之则视为无效。
效果图如下:
代码:
#include <bits/stdc++.h> 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; }
结果: