目录
调整字段宽度:
width(int i)
#include <iostream>
using namespace std;
int main()
{
cout<<"Phone number:";
cout.width(8);
cout<<"2230612-33760"<<endl;
}
运行效果:
填充字符:
fill()
#include <iostream>
using namespace std;
int main()
{
cout.fill('*');
const char * name[2] = {"Name1:","Name2:"};
char * n[2] = {"TOM","Jerry"};
for(int i=0;i<2;i++)
{
cout<<name[i];
cout.width(7);
cout<<n[i];
}
return 0;
}
运行效果:
设置浮点数的显示精度:
precision()
#include <iostream>
using namespace std;
int main()
{
double num=1.91+1.8*2.3/3.1;
cout.precision(5);
cout<<num;
return 0;
}
运行效果:
注意:
width()方法只影响将显示的下一个项目,然后字段宽度将恢复为默认值。
fill()中新的填充字符一直有效,直到被更改为止。
precision()与fill()类似。
完结?
no
未完待更
下一期:必看:你不知道的c++ cout小知识(续)_Sans的头的博客-CSDN博客