#pragma once
#include<iterator>
template<typename Container>
class MyInserterIter : public std::iterator<std::output_iterator_tag, typename Container::value_type>
{
protected:
Container& coll;//注意此处为一个reference
public:
explicit MyInserterIter(Container& c) :coll(c) {}
// MyInserterIter& operator = (const MyInserterIter&) { return *this; }//vs实现中可能需要自己定义拷贝赋值运算符 因为其copy算法的实现调用了拷贝赋值运算符 C++标准中并没有这样的要求
MyInserterIter& operator *()
{
return *this;
}
MyInserterIter& operator ++()
{
return *this;
}
MyInserterIter& operator ++(int)
{
return *this;
}
MyInserterIter& operator =(const typename Container::value_type& value)
{
coll.insert(value);
return *this;
}
};
template<typename coll>
inline MyInserterIter<coll> InserterIter(coll& c)
{
return MyInserterIter<coll>(c);
}