这里有一个细节就是当你在创建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;
}