STL源码分析——istream_iterator(本质上是一个iterator adapter)

在这里插入图片描述
这里有一个细节就是当你在创建istream_iterator的对象(即调用有参构造函数)时,其实已经在等待第一个数据的输入了,因为在有参构造函数的函数体中执行了++运算符。

template<class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
class my_istream_iterator :public iterator<input_iterator_tag, T, Distance, const T*, const T&>//其实就是继承了4个typedef,分别是iterator_category,value_type,difference_type,pointer,reference
{
	basic_istream<charT, traits>*in_stream;
	T value;
public:
	typedef charT char_type;
	typedef traits traits_type;
	typedef basic_istream<charT, traits> istream_type;
	my_istream_iterator() :in_stream(0) {}//constructor
	my_istream_iterator(istream_type&s) :in_stream(&s) { ++*this; }//constructor
	my_istream_iterator(const my_istream_iterator<T, charT, traits, Distance>&x)
		:in_stream(x.in_stream), value(x.value) {}//copy constructor
	~my_istream_iterator() {}
	const T&operator*()const { return value; }
	const T*operator->()const { return &value; }
	my_istream_iterator<T, charT, traits, Distance>&operator++()//前置
	{
		if (in_stream && !(*in_stream >> value))//这里*in_stream >> value会卡在这里等待输入
			in_stream = 0;
		return *this;
	}
	my_istream_iterator<T, charT, traits, Distance> operator++(int)//后置
	{
		my_istream_iterator<T, charT, traits, Distance> temp = *this;
		++*this;
		return temp;
	}
	bool operator!=(my_istream_iterator<T, charT, traits, Distance>&x)//自己写的
	{
		return this->in_stream != x.in_stream;
	}
};



int main()
{
	double value1, value2;
	my_istream_iterator<double>eos;//end-of-stream iterator,调用无参构造函数
	my_istream_iterator<double>itt(cin);//调用有参构造函数
	if (itt != eos)
		value1 = *itt;
	itt++;
	if (itt != eos)
		value2 = *itt;


	cout << value1 << endl;
	cout << value2 << endl;
	system("pause");
	return 0;
}

在这里插入图片描述
结合之前的copy函数模板

template<class InputIterator, class OutputIterator>//函数模板
OutputIterator
my_copy(InputIterator first, InputIterator last, OutputIterator result)
{
	while (first != last)
	{
		*result = *first;//等下会对=进行重载
		++result;
		++first;
	}
	return result;
}
template<class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
class my_istream_iterator :public iterator<input_iterator_tag, T, Distance, const T*, const T&>//其实就是继承了4个typedef,分别是iterator_category,value_type,difference_type,pointer,reference
{
	basic_istream<charT, traits>*in_stream;
	T value;
public:
	typedef charT char_type;
	typedef traits traits_type;
	typedef basic_istream<charT, traits> istream_type;
	my_istream_iterator() :in_stream(0) {}//constructor
	my_istream_iterator(istream_type&s) :in_stream(&s) { ++*this; }//constructor
	my_istream_iterator(const my_istream_iterator<T, charT, traits, Distance>&x)
		:in_stream(x.in_stream), value(x.value) {}//copy constructor
	~my_istream_iterator() {}
	const T&operator*()const { return value; }
	const T*operator->()const { return &value; }
	my_istream_iterator<T, charT, traits, Distance>&operator++()//前置
	{
		if (in_stream && !(*in_stream >> value))//这里*in_stream >> value会卡在这里等待输入
			in_stream = 0;
		return *this;
	}
	my_istream_iterator<T, charT, traits, Distance> operator++(int)//后置
	{
		my_istream_iterator<T, charT, traits, Distance> temp = *this;
		++*this;
		return temp;
	}
	bool operator!=(my_istream_iterator<T, charT, traits, Distance>&x)//自己写的
	{
		return this->in_stream != x.in_stream;
	}
};



int main()
{
	my_istream_iterator<int>eos;
	my_istream_iterator<int>itt(cin);
	vector<int>v;
	my_copy(itt, eos, inserter(v, v.begin()));

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << " " << *it;
	}


	system("pause");
	return 0;
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值