Namespaces
Variants

std:: copy_n

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_n
(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 InputIt, class Size, class OutputIt >
OutputIt copy_n ( InputIt first, Size count, OutputIt result ) ;
(1) (C++11부터)
(C++20부터 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class Size, class ForwardIt2 >
ForwardIt2 copy_n ( ExecutionPolicy && policy,

ForwardIt1 first, Size count, ForwardIt2 result ) ;
(2) (C++17부터)
1) count 개의 값을 first 로 시작하는 범위에서 result 로 시작하는 범위로 정확히 복사합니다. 공식적으로, [ 0 , count ) 범위의 각 정수 i 에 대해 * ( result + i ) = * ( first + i ) 를 수행합니다.
범위의 중첩은 공식적으로 허용되지만, 결과의 예측 불가능한 순서를 초래합니다.
2) (1) 과 동일하지만, policy 에 따라 실행됩니다.
이 오버로드는 다음 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 입니다.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 입니다.

(C++20 이후)

목차

매개변수

first - 복사할 요소 범위의 시작
count - 복사할 요소의 개수
result - 대상 범위의 시작
policy - 사용할 실행 정책
타입 요구사항
-
InputIt LegacyInputIterator 요구사항을 충족해야 함
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 함
-
ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 함

반환값

대상 범위의 반복자로, count > 0 인 경우 복사된 마지막 요소의 다음을 가리키고, 그렇지 않으면 result 를 가리킵니다.

복잡도

count < 0 인 경우 0개의 할당; count 할당 그 외의 경우.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.

가능한 구현

template<class InputIt, class Size, class OutputIt>
constexpr //< since C++20
OutputIt copy_n(InputIt first, Size count, OutputIt result)
{
    if (count > 0)
    {
        *result = *first;
        ++result;
        for (Size i = 1; i != count; ++i, (void)++result)
            *result = *++first;
    }
    return result;
}

예제

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>
int main()
{
    std::string in {"1234567890"};
    std::string out;
    std::copy_n(in.begin(), 4, std::back_inserter(out));
    std::cout << out << '\n';
    std::vector<int> v_in(128);
    std::iota(v_in.begin(), v_in.end(), 1);
    std::vector<int> v_out(v_in.size());
    std::copy_n(v_in.cbegin(), 100, v_out.begin());
    std::cout << std::accumulate(v_out.begin(), v_out.end(), 0) << '\n';
}

출력:

1234
5050

참고 항목

요소 범위를 새로운 위치로 복사합니다
(함수 템플릿)
지정된 개수의 요소를 새로운 위치로 복사합니다
(알고리즘 함수 객체)