std::ranges::copy, std::ranges::copy_if, std::ranges::copy_result, std::ranges::copy_if_result
| 헤더에 정의됨 <algorithm>
|
||
| 호출 시그니처 |
||
template< std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O >
requires std::indirectly_copyable<I, O>
constexpr copy_result<I, O>
copy( I first, S last, O result );
|
(1) | (C++20부터) |
template< ranges::input_range R, std::weakly_incrementable O >
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr copy_result<ranges::borrowed_iterator_t<R>, O>
copy( R&& r, O result );
|
(2) | (C++20부터) |
template< std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
requires std::indirectly_copyable<I, O>
constexpr copy_if_result<I, O>
copy_if( I first, S last, O result, Pred pred, Proj proj = {} );
|
(3) | (C++20부터) |
template< ranges::input_range R, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>, Proj>> Pred >
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr copy_if_result<ranges::borrowed_iterator_t<R>, O>
copy_if( R&& r, O result, Pred pred, Proj proj = {} );
|
(4) | (C++20부터) |
| 도우미 형식 |
||
template< class I, class O >
using copy_result = ranges::in_out_result<I, O>;
|
(5) | (C++20부터) |
template< class I, class O >
using copy_if_result = ranges::in_out_result<I, O>;
|
(6) | (C++20부터) |
다음으로 정의된 범위의 요소를 복사합니다:[first, last)에서 시작하는 다른 범위로 복사합니다.result.
[first, last)에서 시작하여first로 진행합니다.last - 1의 동작은 정의되지 않습니다: 만약result가 범위 내에 있으면[first, last)입니다. 이 경우,ranges::copy_backward를 대신 사용할 수 있습니다.pred가 반환하는 요소만 복사합니다.true복사된 요소들의 상대적인 순서는 유지됩니다.r이지만, ranges::begin(r)을 소스 범위로 사용하며, 마치 first을 ranges::end(r)로 사용하고 last을. 이 페이지에서 설명하는 함수와 같은 개체는알고리즘 함수 객체(비공식적으로니블로이드
- 라고 함)입니다. 즉:
- 명시적 템플릿 인수 목록은 이러한 함수를 호출할 때 지정할 수 없습니다.이들 중 어느 것도인수 종속 조회
- 에 보이지 않습니다.이들 중 하나가 함수 호출 연산자의 왼쪽에 있는 이름으로일반적인 비한정 조회에 의해 발견되면,인수 종속 조회
매개변수
| first, last | - | 복사할 요소들의 범위 를 정의하는 반복자-감시자 쌍 |
| r | - | 복사할 요소들의 범위 |
| result | - | 대상 범위의 시작 지점 |
| pred | - | 투영된 요소들에 적용할 조건자 |
| proj | - | 요소들에 적용할 투영 |
반환값
ranges::in_out_result 를 포함하며, 입력 반복자는 last 와 같고 출력 반복자는 복사된 마지막 요소의 다음을 가리키는 객체입니다.
복잡도
참고 사항
실제로,
ranges::copy
구현체는 다중 할당을 피하고 값 타입이
TriviallyCopyable
이고 반복자 타입이
contiguous_iterator
를 만족하는 경우
std::memmove
와 같은 벌크 복사 함수를 사용합니다.
겹치는 범위를 복사할 때,
ranges::copy
는 왼쪽으로 복사할 때(대상 범위의 시작이 원본 범위 밖에 있을 때) 적합한 반면,
ranges::copy_backward
는 오른쪽으로 복사할 때(대상 범위의 끝이 원본 범위 밖에 있을 때) 적합합니다.
가능한 구현
| copy (1)(2) |
|---|
struct copy_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O> requires std::indirectly_copyable<I, O> constexpr ranges::copy_result<I, O> operator()(I first, S last, O result) const { for (; first != last; ++first, (void)++result) *result = *first; return {std::move(first), std::move(result)}; } template<ranges::input_range R, std::weakly_incrementable O> requires std::indirectly_copyable<ranges::iterator_t<R>, O> constexpr ranges::copy_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(result)); } }; inline constexpr copy_fn copy; |
| copy_if (3)(4) |
struct copy_if_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> requires std::indirectly_copyable<I, O> constexpr ranges::copy_if_result<I, O> operator()(I first, S last, O result, Pred pred, Proj proj = {}) const { for (; first != last; ++first) if (std::invoke(pred, std::invoke(proj, *first))) { *result = *first; ++result; } return {std::move(first), std::move(result)}; } template<ranges::input_range R, std::weakly_incrementable O, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>, Proj>> Pred> requires std::indirectly_copyable<ranges::iterator_t<R>, O> constexpr ranges::copy_if_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result, Pred pred, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(result), std::ref(pred), std::ref(proj)); } }; inline constexpr copy_if_fn copy_if; |
예제
다음 코드는
ranges::copy
를 사용하여 하나의
std::vector
내용을 다른
std::vector
로 복사하고 결과를 출력합니다.
#include <algorithm> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector<int> source(10); std::iota(source.begin(), source.end(), 0); std::vector<int> destination; std::ranges::copy(source.begin(), source.end(), std::back_inserter(destination)); // 또는 대안으로 // std::vector<int> destination(source.size()); // std::ranges::copy(source.begin(), source.end(), destination.begin()); // 두 방법 모두 다음 코드와 동등함 // std::vector<int> destination = source; std::cout << "Destination contains: "; std::ranges::copy(destination, std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; std::cout << "Odd numbers in destination are: "; std::ranges::copy_if(destination, std::ostream_iterator<int>(std::cout, " "), [](int x) { return (x % 2) == 1; }); std::cout << '\n'; }
출력:
Destination contains: 0 1 2 3 4 5 6 7 8 9 Odd numbers in destination are: 1 3 5 7 9
참고 항목
|
(C++20)
|
요소 범위를 역순으로 복사합니다
(알고리즘 함수 객체) |
|
(C++20)
|
뒤집힌 범위의 복사본을 생성합니다
(알고리즘 함수 객체) |
|
(C++20)
|
지정된 개수의 요소를 새 위치로 복사합니다
(알고리즘 함수 객체) |
|
(C++20)
|
요소 범위에 특정 값을 할당합니다
(알고리즘 함수 객체) |
|
(C++20)
(C++20)
|
특정 조건을 만족하는 요소를 제외하고 범위를 복사합니다
(알고리즘 함수 객체) |
|
(C++11)
|
요소 범위를 새 위치로 복사합니다
(함수 템플릿) |