Namespaces
Variants

std:: copy_backward

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
copy_backward
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class BidirIt1, class BidirIt2 >
BidirIt2 copy_backward ( BidirIt1 first, BidirIt1 last, BidirIt2 d_last ) ;
(C++20부터 constexpr)

범위 [ first , last ) 의 요소들을 d_last 로 끝나는 다른 범위로 복사합니다. 요소들은 역순으로(마지막 요소가 먼저 복사됨) 복사되지만, 상대적 순서는 유지됩니다.

d_last ( first , last ] 범위 내에 있으면 동작은 정의되지 않습니다. 이 경우 std::copy_backward 대신 std::copy 를 사용해야 합니다.

목차

매개변수

first, last - 복사할 요소들의 소스 범위 를 정의하는 반복자 쌍
d_last - 대상 범위의 끝
타입 요구사항
-
BidirIt LegacyBidirectionalIterator 요구사항을 충족해야 합니다.

반환값

복사된 마지막 요소에 대한 반복자.

복잡도

정확히 std:: distance ( first, last ) 번의 할당이 수행됩니다.

참고 사항

겹치는 범위를 복사할 때, std::copy 는 왼쪽으로 복사할 때(대상 범위의 시작이 소스 범위 밖에 있을 때) 적합한 반면, std::copy_backward 는 오른쪽으로 복사할 때(대상 범위의 끝이 소스 범위 밖에 있을 때) 적합합니다.

가능한 구현

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

예제

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
int main()
{
    std::vector<int> source(4);
    std::iota(source.begin(), source.end(), 1); // 1, 2, 3, 4로 채움
    std::vector<int> destination(6);
    std::copy_backward(source.begin(), source.end(), destination.end());
    std::cout << "destination contains: ";
    for (auto i: destination)
        std::cout << i << ' ';
    std::cout << '\n';
}

출력:

destination contains: 0 0 1 2 3 4

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 1206 C++98 1. 다음 경우 동작이 정의되어 있었음 d_last == last
2. 다음 경우 동작이 정의되지 않았음 d_last == first
1. 정의되지 않음으로 변경
2. 정의됨으로 변경

참고 항목

요소 범위를 새로운 위치로 복사합니다
(function template)
요소 범위를 역순으로 복사합니다
(algorithm function object)