34~ C++ 输出流

1. 输出流对象:cout

class std::ostream;
object std::cout
extern ostream cout;

1.1 数据输出

1)operator << : 格式化输出

  • 输出字符串
#include <iostream>
using namespace std;

int main()
{
	cout << "Hello World! " << endl;
	return 0;
}
  • 进制转换
#include <iostream>
using namespace std;

int main()
{
	int i = 169,j=144;
	
	cout << i << endl;
	cout << j << endl;

	cout << hex;
	cout << i << endl;
	cout << j << endl;

	cout << oct;
	cout << i  << endl;
	cout << j << endl;

	cout << dec;
	cout << i  << endl;
	cout << j << endl;
	return 0;
}
  • 数字对齐
#include <iostream>
using namespace std;
/*
输出:
:    H
:Hello
:Hello World
:   99

*/
int main()
{
	cout <<":";
	cout.width(5);
	cout << "H"<< endl;

	cout <<":";
	cout.width(5);
	cout << "Hello" << endl;

	cout <<":";
	cout.width(5);
	cout << "Hello World" << endl;

	cout <<":";
	cout.width(5);
	cout << 99 << endl;
	return 0;
}
  • 填充字符
/*
输出:
:****H
:Hello
:Hello World
:***99

*/
#include <iostream>
using namespace std;

int main()
{
	cout.fill('*');

	cout <<":";
	cout.width(5);
	cout << "H"<< endl;

	cout <<":";
	cout.width(5);
	cout << "Hello" << endl;

	cout <<":";
	cout.width(5);
	cout << "Hello World" << endl;

	cout <<":";
	cout.width(5);
	cout << 99 << endl;
	return 0;
}
  • cout 格式化函数

(1) fmtflags setf (fmtflags fmtfl);

#include <iostream>
using namespace std;

int main()
{
	bool assert = false;
	cout << assert << endl; /*0*/

	cout.setf(ios_base::boolalpha);
	cout <<  assert<< endl; /*false*/
	cout.unsetf(ios_base::boolalpha);
	

	cout << "----------------" << endl;
	int val = 100;
	cout << val << endl; /*100*/
	cout << hex << val << endl;/*64*/
	cout << oct << val << endl;/*144*/

	cout.setf(ios_base::showbase);
	cout << val << endl; /*100*/
	cout << hex << val << endl; /*0x64*/ 
	cout << oct << val << endl; /*0144*/
	cout << dec;
	cout.unsetf(ios_base::showbase);

	cout << "-----------------" << endl;
	float fval = 80;
	cout << fval << endl; /*80*/
	cout.setf(ios_base::showpoint);
	cout << fval << endl; /*80.0000*/
	cout.unsetf(ios_base::showpoint);

	
	cout << "-----------------" << endl;	
	cout.setf(ios_base::showpos);
	cout << 100 << endl;/*+100*/
	cout << 80.99 << endl;/*+80.99*/
	cout.unsetf(ios_base::showpos);

	
	cout << "-----------------" << endl;	
	cout << hex << 999 << endl;/*3e7*/
	cout.setf(ios_base::uppercase);
	cout << hex << 999 << endl;/*3E7*/
	cout.unsetf(ios_base::uppercase);

	/*
	unitbuf	flush output after each inserting operation.
	*/
	cout << "-----------------" << endl;	
	cout.setf(ios_base::unitbuf);
	cout << 100;
	cout.unsetf(ios_base::unitbuf);

	return 0;
}

(2) fmtflags setf (fmtflags fmtfl, fmtflags mask);
在这里插入图片描述

/*代码略*/

2)ostream::put :单字符输出

3) ostream::write :块输出

1.2 刷新流缓冲

#include <iostream>
using namespace std;

int main()
{
	cout.put('C');
	cout << flush;
	
	cout.put('D');
	flush(cout);

	cout.put('E');
	cout << endl;
	return 0;
}

2. 附图:IO流继承谱系

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值