函数主要用来设置域宽 ,只对紧接着的输出有效,紧接着的输出结束后又变回默认的域宽
包含在头文件#include<iomanip>
iomanip的作用:
主要是对cin,cout的一些操作运算,
cout<<setw(3)<<a;和printf("%3d",a);是等效的
#include<iostream>
#include<iomanip>
using namespace std;
class text
{
public:
text(int a,int b)
{
cout << a << "+" << b << "=" << a + b << endl;
cout << setw(7) << a << endl << "+" << setw(6) << b << endl<<"-------"<<endl << setw(7) << a + b << endl;
}
};
int main()
{
int a = 0, b = 0;
cin >> a >> b;
text t(a, b);
return 0;
}