输入流迭代器istream_iterator的使用

最近在看泛型编程的知识,发现了一个知识盲区,就是iostream迭代器。

现在使用其中的istream_iterator输入流迭代器来看一看,代码如下:

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
using std::vector;

int main(int argc, char *argv[])
{
	istream_iterator<int> int_in(cin);
	istream_iterator<int> int_eof;

	vector<int> vecInts;

	while (int_in != int_eof)
	{
		vecInts.push_back(*int_in++);
	}
		
	//以上几行也可以改为如下形式,两种写法等价
	//vector<int> vecInts(int_in, int_eof);

	for (const int& data : vecInts)
	{
		cout << data << ends;
	}
}

运行结果如下:
在这里插入图片描述
对于这玩意能跑,我是真的深表诧异,对于我的代码观有了一个强烈的冲击。

先用这篇博客记录一下,提醒自己不会的还有很多,一点不能自满。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值