|Category: algorithms ||Component type: function |
template <class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 copy_backward(BidirectionalIterator1 first,
Copy_backward copies elements from the range
[first, last) to the range
[result - (last - first), result) . That is, it performs the assignments
*(result - 1) = *(last - 1),
*(result - 2) = *(last - 2), and so on. Generally, for every integer
last - first,
copy_backward performs the assignment
*(result - n - 1) = *(last - n - 1). Assignments are performed from the end of the input sequence to the beginning, i.e. in order of increasing
The return value is
result - (last - first)
Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.
Requirements on types
BidirectionalIterator1 and BidirectionalIterator2 are models of BidirectionalIterator.
BidirectionalIterator1's value type is convertible to BidirectionalIterator2's value type.
[first, last) is a valid range.
result is not an iterator within the range
There is enough space to hold all of the elements being copied. More formally, the requirement is that
[result - (last - first), result) is a valid range.
last - first assignments are performed.
iota(V.begin(), V.end(), 1);
copy_backward(V.begin(), V.begin() + 10, V.begin() + 15);
Result is an iterator that points to the end of the output range. This is highly unusual: in all other STL algorithms that denote an output range by a single iterator, that iterator points to the beginning of the range.
 The order of assignments matters in the case where the input and output ranges overlap:
copy_backward may not be used if
result is in the range
[first, last). That is, it may not be used if the end of the output range overlaps with the input range, but it may be used if the beginning of the output range overlaps with the input range;
copy has opposite restrictions. If the two ranges are completely nonoverlapping, of course, then either algorithm may be used.