STL: copy

copy

Assigns the values of elements from a source range to a destination range, iterating through the source sequence of elements and assigning them new positions in a forward direction.

template<class InputIterator, class OutputIterator>
   OutputIterator copy(
      InputIterator _First, 
      InputIterator _Last, 
      OutputIterator _DestBeg
   );

注,Because the algorithm copies the source elements in order beginning with the first element, the destination range can overlap with the source range provided the _Last position of the source range is not contained in the destination range. copy can be used to shift elements to the left but not the right, unless there is no overlap between the source and destination ranges. To shift to the right any number of positions, use the copy_backward algorithm.

 

copy_backward

Assigns the values of elements from a source range to a destination range, iterating through the source sequence of elements and assigning them new positions in a backward direction.

template<class BidirectionalIterator1, class BidirectionalIterator2>
   BidirectionalIterator2 copy_backward(
      BidirectionalIterator1 _First, 
      BidirectionalIterator1 _Last,
      BidirectionalIterator2 _DestEnd
   );

注,Because the algorithm copies the source elements in order beginning with the last element, the destination range can overlap with the source range provided the _First position of the source range is not contained in the destination range. copy_backward can be used to shift elements to the right but not the left, unless there is no overlap between the source and destination ranges. To shift to the left any number of positions, use the copy algorithm.

 

注,当源区间和目的区间之间没有重叠时,copy和copy_backward都可以正确工作。但是当_First<=_DestBeg<_Last时,copy由于从左向右复制元素会覆盖源区间,无法正常工作。而这时只能使用copy_backward.但是当_First<=_DestEnd<_Last时,copy_backward由于从右向左复制元素会覆盖源区间,无法正常工作。而这时只能使用copy。

此外,对于数组可以直接使用速度最快的memcpy和memmove。其中,memmove确保了存在覆盖情况下也可以正确复制。

 

copy_if

Copy all elements in a given range that test true for a specified condition.

template<class InputIterator, class OutputIterator, class BinaryPredicate>
    OutputIterator copy_if(
        InputIterator _First, 
        InputIterator _Last,
        BinaryPredicate _Comp

 

copy_n

Copies a specified number of elements.

template<class InputIterator, class Size, class OutputIterator>
    OutputIterator copy_n(
        InputIterator  _First, 
        Size _Count,
        OutputIterator _Dest

 

 

posted @ 2013-03-07 14:48  freewater  阅读(614)  评论(0编辑  收藏  举报