初學者在使用 C++ 輸出格式化時,常誤以為存在 stew 函數,即使包含了頭文件並使用命名空間,編譯器仍報錯「未定義」。
C++ 標準庫中並無 stew 函數。你真正需要的是 <iomanip> 頭文件中的 setw(int n),用於設置下一個輸出項目的最小寬度。若實際輸出內容寬度不足,會自動填充空格,默認右對齊。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int a = 0, b = 10, c = 100;
cout << setw(3) << a << endl;
cout << setw(3) << b << endl;
cout << setw(3) << c << endl;
system("pause");
return 0;
}
Visual Studio 2013
#include <iomanip>setw(寬度) 僅對緊隨其后的一次輸出有效setfill('*') 可自定義填充字符