转:
插入迭代器,顾名思义,插入用的;比如对于algorithm中的copy函数,
1
2
3
4
5
6
7
8
9
template<class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
while (first!=last) {
*result = *first;
++result; ++first;
}
return result;
}
是将[first, last)之间的元素插入到result指向的位置前or后,目标容器空间可能大于、等于或小于 [first, last) 这个范围,所以对于OutputIterator类型的迭代器,要满足在++时,空间不溢出(OutputIterator可以是除三种插入迭代器外的其他多种迭代器类型);
对于三种插入迭代器,它们继承自iterator,同时各自重载了赋值操作符,不同的插入迭代器在赋值操作符中分别调用了push_front,push_back或者insert成员函数,这样插入迭代器就可以保证目标容器的空间不溢出;
back_insert_iterator:
是在容器的尾部插入元素,调用的是容器的push_back成员函数;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class Container>
class back_insert_iterator :
public iterator<output_iterator_tag,void,void,void,void>
{
protected:
Container* container;
public:
typedef Container container_type;
explicit back_insert_iterator (Container& x) : container(std::addressof(x)) {}
back_insert_iterator<Container>& operator= (const typename Container::value_type& value)
{ container->push_back(value); return *this; }
back_insert_iterator<Container>& operator= (typename Container::value_type&& value)
{ container->push_back(std::move(value)); return *this; }
back_insert_iterator<Container>& operator* ()
{ return *this; }
back_insert_iterator<Container>& operator++ ()
{ return *this; }
back_insert_iterator<Container> operator++ (int)
{ return *this; }
};
front_insert_iterator:
是在容器的头部插入元素,调用的是容器的push_front成员函数;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class Container>
class front_insert_iterator :
public iterator<output_iterator_tag,void,void,void,void>
{
protected:
Container* container;
public:
typedef Container container_type;
explicit front_insert_iterator (Container& x) : container(std::addressof(x)) {}
front_insert_iterator<Container>& operator= (const typename Container::value_type& value)
{ container->push_front(value); return *this; }
front_insert_iterator<Container>& operator= (typename Container::value_type&& value)
{ container->push_front(std::move(value)); return *this; }
front_insert_iterator<Container>& operator* ()
{ return *this; }
front_insert_iterator<Container>& operator++ ()
{ return *this; }
front_insert_iterator<Container> operator++ (int)
{ return *this; }
};
insert_iterator:
是在容器的中间插入元素,调用的是容器的insert成员函数;
所以在初始化insert_iterator的时候需要指定要插入的位置;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class Container>
class insert_iterator :
public iterator<output_iterator_tag,void,void,void,void>
{
protected:
Container* container;
typename Container::iterator iter;
public:
typedef Container container_type;
explicit insert_iterator (Container& x, typename Container::iterator i)
: container(std::addressof(x)), iter(i) {}
insert_iterator<Container>& operator= (const typename Container::value_type& value)
{ iter=container->insert(iter,value); ++iter; return *this; }
insert_iterator<Container>& operator= (typename Container::value_type&& value)
{ iter=container->insert(iter,std::move(value)); ++iter; return *this; }
insert_iterator<Container>& operator* ()
{ return *this; }
insert_iterator<Container>& operator++ ()
{ return *this; }
insert_iterator<Container> operator++ (int)
{ return *this; }
};
template <class Container, class Iterator>
insert_iterator<Container> inserter (Container& x, Iterator it);
template <class Container>
front_insert_iterator<Container> front_inserter (Container& x);
template <class Container>
back_insert_iterator<Container> back_inserter (Container& x);
分别用来产生三种插入迭代器;