cout输出格式控制

#include 
#include 
#include 
#include
using namespace std;
int main()
{
	//ostream::fmtflags old = cout.flag();        // 无参将返回当前 flag 值
	//cout.flag(old);
	//cout.


	cout << "numeric : " << true << " or " << false << endl;              // 1 or 0
	cout << "literals : " << boolalpha << true << " or " << false << endl; // true or false
	cout << "literals : " << boolalpha << 0 << endl;                     // 0    原因: 0 在cout中不等价于 false
	cout << "numeric : " << noboolalpha << true << " or " << false << endl;// 1 or 0
	cout << endl << endl;

	//====================================================================================================//

	const int ival = 17;        // 'ival' is constant, so value never change
	cout << "oct : " << oct << ival << endl;        // 21 : 8 进制
	cout << "dec : " << dec << ival << endl;        // 17 : 10 进制
	cout << "hex : " << hex << ival << endl;        // 11 : 16 进制
	cout << "hex : " << hex << 17.01 << endl;        // 17.01 : 不受影响

	cout << endl;
	cout << showbase;                            // Show base when printing integral values
	cout << "oct : " << oct << ival << endl;        // 21 : 8 进制
	cout << "dec : " << dec << ival << endl;        // 017 : 10 进制
	cout << "hex : " << hex << ival << endl;        // 0x11 : 16 进制
	cout << "hex : " << hex << 17.01 << endl;        // 17.01 : 不受影响
	cout << noshowbase;                            // Reset state of the stream

	cout << endl;
	cout << showbase << uppercase;
	cout << "hex : " << hex << 15 << endl;            // 0XF 大写形式
	cout << nouppercase;
	cout << "hex : " << hex << 15 << endl;            // 0xf 小写形式
	cout << dec;
	
	cout << ival << endl << endl << endl;

	//======================================================================================//

	// cout.pricision(4) ;                         // 等价于 cout <

你可能感兴趣的:(cout输出格式控制)