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; }
結果:

