copy:将源区间中的所有元素复制到dest为起点中去.
template<class _InIt,
class _OutIt> inline
_OutIt copy(_InIt _First, _InIt _Last,
_OutIt _Dest)
{ // copy [_First, _Last) to [_Dest, ...)
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Dest);
return (_Copy_impl(_Unchecked(_First), _Unchecked(_Last),
_Dest, _Is_checked(_Dest)));
}
template<class _InIt,
class _OutIt> inline
_OutIt _Copy_impl(_InIt _First, _InIt _Last,
_OutIt _Dest, _STD tr1::true_type)
{ // copy [_First, _Last) to [_Dest, ...), checked dest
return (_Copy_impl(_First, _Last,
_Dest, _Iter_cat(_First), _Iter_cat(_Dest)));
}
template<class _InIt,
class _OutIt> inline
_OutIt _Copy_impl(_InIt _First, _InIt _Last,
_OutIt _Dest, input_iterator_tag, output_iterator_tag)
{ // copy [_First, _Last) to [_Dest, ...), arbitrary iterators
return (_Copy_impl(_First, _Last,
_Dest));
}
template<class _InIt,
class _OutIt> inline
_OutIt _Copy_impl(_InIt _First, _InIt _Last,
_OutIt _Dest)
{ // copy [_First, _Last) to [_Dest, ...)
return (_Copy_impl(_First, _Last,
_Dest, _Ptr_cat(_First, _Dest)));
}
// TEMPLATE FUNCTION copy
template<class _InIt,
class _OutIt> inline
_OutIt _Copy_impl(_InIt _First, _InIt _Last,
_OutIt _Dest, _Nonscalar_ptr_iterator_tag)
{ // copy [_First, _Last) to [_Dest, ...), arbitrary iterators
for (; _First != _Last; ++_Dest, ++_First)
*_Dest = *_First;
return (_Dest);
}
这个函数比较简单.但是如果将其和back_inserter连接起来就可以向空容器中复制元素了.关于back_inserter的源码我们可以参考之前的那篇文章:http://blog.csdn.net/yuanweihuayan/article/details/7526389
template<class _BidIt1,
class _BidIt2> inline
_BidIt2 copy_backward(_BidIt1 _First, _BidIt1 _Last,
_BidIt2 _Dest)
{ // copy [_First, _Last) backwards to [..., _Dest)
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Dest);
return (_Copy_backward(_Unchecked(_First), _Unchecked(_Last),
_Dest, _Is_checked(_Dest)));
}
template<class _BidIt1,
class _BidIt2> inline
_BidIt2 _Copy_backward(_BidIt1 _First, _BidIt1 _Last,
_BidIt2 _Dest, _STD tr1::true_type)
{ // copy [_First, _Last) backwards to [..., _Dest), checked dest
return (_Copy_backward(_Unchecked(_First), _Unchecked(_Last),
_Dest));
}
template<class _BidIt1,
class _BidIt2> inline
_BidIt2 _Copy_backward(_BidIt1 _First, _BidIt1 _Last,
_BidIt2 _Dest)
{ // copy [_First, _Last) backwards to [..., _Dest), unchecked
return (_Copy_backward(_First, _Last,
_Dest, _Ptr_cat(_First, _Dest)));
}
// TEMPLATE FUNCTION copy_backward
template<class _BidIt1,
class _BidIt2> inline
_BidIt2 _Copy_backward(_BidIt1 _First, _BidIt1 _Last,
_BidIt2 _Dest, _Nonscalar_ptr_iterator_tag)
{ // copy [_First, _Last) backwards to [..., _Dest), arbitrary iterators
while (_First != _Last)
*--_Dest = *--_Last;//注意此处是以递减的形式复制元素
return (_Dest);
}
举例:
1. int main()
2. {
3. vector<int> vecInt;
4. for ( int i = 0;i < 10;++ i)
5. {
6. vecInt.push_back( i );
7. }
8.
9. vector<int> lstInt;
10. copy( vecInt.begin(),vecInt.end(),back_inserter( lstInt ) );
11. copy( lstInt.begin(),lstInt.end(),ostream_iterator<int>( cout," " ) );
12. cout<<"\ncopy backward:\n";
13. copy_backward( vecInt.begin() + 3,vecInt.begin() + 6,lstInt.begin() + 7 );
14. copy( lstInt.begin(),lstInt.end(),ostream_iterator<int>( cout," " ) );
15. system( "pause" );
16. return 0;
17. }
18.
输出:
0 1 2 3 4 5 6 7 8 9
copy backward:
0 1 2 3 3 4 5 7 8 9
使用copy莫过于代码11所示那样.