C++设置输出域宽与设置标志

使用set iOS flags(ios_base::right); 设置标志

通过resetiosflags(ios_base::right);  清楚设置标志,只有清楚设置标志之后才能重新设置

#include <iostream>
#include<iomanip>

using namespace std;

void main()
{
	cout << setfill('*')
		<< setw(0) << 15 << endl
		<< setw(1) << 15 << endl
		<< setw(2) << 15 << endl
		<< setw(3) << 15 << endl
		<< setw(4) << 15 << endl;
	cout << setw(16) << setfill('*') << "" << endl;
	cout << setiosflags(ios_base::right)    //按输出域右边对齐输出
		<< setw(5) << 1
		<< setw(5) << 2
		<< setw(5) << 3 << endl;
	cout << resetiosflags(ios_base::right);
	cout << setiosflags(ios_base::left)        //按输出域左边边对齐输出
		<< setw(5) << 1
		<< setw(5) << 2
		<< setw(5) << 3 << endl;
}

显示结果

发布了80 篇原创文章 · 获赞 32 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/dopdkfsds/article/details/100064749