next up previous index
Next: Exchange values or Up: Mutating sequence operations Previous: Mutating sequence operations

Copy one sequence into another sequence

Source code

Declaration

template <class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last,
                    OutputIterator result);

template <class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward(BidirectionalIterator1 first,
                                     BidirectionalIterator1 last,
                                     BidirectionalIterator2 result);

Description  The copy  function copies  all of the elements in the range [ first, last) to the range of size n beginning at result , where n is the size of the range [ first, last) , and returns the past-the-end iterator. For copy , the source and destination ranges may overlap  if result first.

The copy_backward  function copies  all of the values in the range [ first, last) to the range of size n ending at result , where n is the size of the range [ first, last) , and returns the iterator that contains the last element copied (i.e. the beginning of the sequence). For copy_backward , the source and destination ranges may overlapcopy!overlapping ranges if result last.

Type requirements

Group Mutating sequence operations.

Time complexity Linear.

The number of type T assignment operations performed is n, where T is the type of the sequence values.

Space complexity Constant.

Mutative? Yes.

Details

  1. One of the uses of the copy function is shifting  a sequence one or more locations to the left (when OutputIterator is the same type as InputIterator and result is to the left of first).
  2. One of the uses of the copy_backward function is shifting  a sequence one or more locations to the right (when BidirectionalIterator1 is the same type as BidirectionalIterator2 and result is to the right of last). (``Backward''   refers to the direction of the iteration, not to the direction of the move.)
  3. Since the types, InputIterator and OutputIterator for copy and BidirectionalIterator1 and BidirectionalIterator2 for copy_backward, need not be the same, this function can be used for converting from one sequence representation   (such as array) to another (such as linked-list or doubly-linked-list).

Example Select here to view the source code for the example.

 

Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.



next up previous index
Next: Exchange values or Up: Mutating sequence operations Previous: Mutating sequence operations



Kenny Zalewski
Mon May 13 04:03:40 EDT 1996