std::copy_backward

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>

template< class BidirectionalIterator1, class BidirectionalIterator2 >

BidirectionalIterator2 copy_backward( BidirectionalIterator1 first,
                                      BidirectionalIterator1 last,

                                      BidirectionalIterator2 d_last );

Copies the elements from the range, defined by [first, last), to another range ending at d_last. The elements are copied in reverse order.

Contents

Parameters

first, last - the range of the elements to copy
d_last - end of the destination range

Return value

iterator to the last element copied.

Complexity

linear in the distance between first and last

Equivalent function

template< class BidirectionalIterator1, class BidirectionalIterator2 >
BidirectionalIterator2 copy_backward(BidirectionalIterator1 first,
                                     BidirectionalIterator1 last,
                                     BidirectionalIterator2 d_last)
{
    while (first != last) {
        *(--d_last) = *(--last);
    }
    return d_last;
}

Example

#include <algorithm>
#include <iostream>
 
int main()
{
    std::vector<int> from_vector;
    for (int i = 0; i < 10; i++) {
        from_vector.push_back(i);
    }
 
    std::vector<int> to_vector(15);
 
    std::copy_backward(from_vector.begin(), from_vector.end(), to_vector.end());
 
    std::cout << "to_vector contains: ";
    for (unsigned int i = 0; i < to_vector.size(); i++) {
        std""cout << to_vector[i] << " ";
    }
 
    return 0;
}

Output:

​to_vector contains: 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9​

See also

copy
copies some range of elements to a new location
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages