#include <iostream> #include<iomanip> using namespace std; int main() { int a = 9999; int b = 999; int c = 99; //默认右对齐 cout << "默认右对齐为: "; cout << setw(4) << a << endl; cout << setw(4) << b << endl; cout << setw(4) << c << endl; //现在改为左对齐 cout << " 改为左对齐后为: "; cout << left << setw(4) << a << endl; cout << left << setw(4) << b << endl; cout << left << setw(4) << c << endl; return 0; }
输出对齐需要包含头文件<iomanip>,当然对齐方式也分为左右两种,直接看代码更好理解