## CSDN博客

### Iterators

Iterators

1.Input Iterators

1)prefix++ and postfix++
2)operator!=
3)dereference* and only support read, not support write
4) operator ==
see in msdn
template< typename InputIterator > find(....)表明find算法需要Input Iterator。
see below:
template< typename T, typename InputIterator >
InputIterator find( InputIterator first, InputIterator last, const T& value )
{
while ( first != last && *first !=value )
{
++first ;
}
return first ;
}

2.Output Iterators

1)perfix++ and postfix++
2)dereference*, only support write
see below:
template< typename InputIterator, typename OutputIterator >
OutputIterator copy( InputIterator first, InputIterator last, OutputIterator result )
{
while ( first != last )
{
*result = *first ;
++first ;
++result ;
}
return result ;
}

3.Forward Iterators

multipass algorithm中。
see below:
template< typename ForwardIterator, typename T >
void replace< ForwardIterator first, ForwardIterator last, const T& x, const T& y )
{
while ( first != last )
{
if ( *first == x )
*first = y ;
++first ;
}
}

4.Bidirectional Iterators

such algorithm reverse require Bidirectional Iterators

5.Random access Iterators

1)r+n和r-n
2)r[n]，即*(r+n)
3)双向跳跃，即r+=n和r-=n
4)operator- with random access iterator，即r-s，得到一整数值
5)operator comparisons between random access iterator，即r<s，r>s，r<=s，r>=s，得到一bool值
vector、deque的iterator满足以上这些要求。

6.Insert Iterator
1)back_insert_iterator<Container> 利用push_back成员函数
2)front_insert_iterator<Container> 利用push_front成员函数
3)insert_iterator<Container> 利用insert成员函数

template< typename Container>
inline back_insert_iterator<Container> back_inserter(Container& x)
{
return back_insert_iterator<Container>(x) ;
}

contain.push_back(value) ;

7.istream_iterator和ostream_iterator

0 0