Namespaces
Variants

std::ranges::uninitialized_copy_n, std::ranges::uninitialized_copy_n_result

cppreference.net에서
 
 
알고리즘 라이브러리
제약된 알고리즘과 범위에 대한 알고리즘 (C++20)
제약된 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 검색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
 
헤더 <memory>
호출 시그니처
template< std::input_iterator I,
          /*nothrow-input-iterator*/ O, /*nothrow-sentinel-for*/<O> S >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_reference_t<I>>
uninitialized_copy_n_result<I, O>
    uninitialized_copy_n( I ifirst, std::iter_difference_t<I> count,
                          O ofirst, S olast );
(1) (C++20부터)
(C++26부터 constexpr)
template< /*execution-policy*/ Ep, std::random_access_iterator I,
          /*nothrow-random-access-iterator*/ O,
          /*nothrow-sized-sentinel-for*/<O> S>
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_reference_t<I>>
uninitialized_copy_n_result<I, O>
    uninitialized_copy_n( Ep&& policy,
                          I ifirst, std::iter_difference_t<I> count,
                          O ofirst, S olast );
(2) (C++26부터)
도우미 타입
template< class I, class O >
using uninitialized_copy_n_result = ranges::in_out_result<I, O>;
(3) (C++20부터)

의 정의는 /*execution-policy*/이 페이지를 참조하십시오; 다른 설명 전용 개념의 정의는 이 페이지를 참조하십시오.

1) 다음과 같이 소스 범위 [ofirst, olast)의 요소를 대상 범위 ifirst + [0, count)에 요소를 생성합니다.

auto ret = ranges::uninitialized_copy(std::counted_iterator(std::move(ifirst), count),
                                      std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};

초기화 중 예외가 발생하면, 이미 생성된 객체들은 지정되지 않은 순서로 소멸됩니다.
[ofirst, olast)이 ifirst + [0, count)와 겹치는 경우 동작은 정의되지 않습니다.
2) (1)과 동일하지만, 에 따라 실행됩니다.이 페이지에 설명된 함수-유사 엔티티는 policy알고리즘 함수 객체

(비공식적으로 niebloids라고 함)입니다. 즉: 이들 중 하나를 호출할 때 명시적 템플릿 인자 목록을 지정할 수 없습니다.이들 중 어느 것도

매개변수

ifirst - 복사할 요소의 범위의 시작
count - 복사할 요소의 개수
ofirst, olast - 대상 요소의 범위를 정의하는 반복자-센티넬 쌍
policy - 사용할 실행 정책

반환값

위에서 설명한 대로입니다.

예외

대상 범위의 요소를 생성할 때 던져지는 모든 예외.

2) 실행 과정 중:
  • 병렬화에 필요한 임시 메모리 리소스를 사용할 수 없는 경우, std::bad_alloc이 던져집니다.
  • 알고리즘 인수를 통해 객체에 접근하는 동안 처리되지 않은 예외가 던져지면, 동작은 실행 정책에 따라 결정됩니다 (표준 정책의 경우 std::terminate이 호출됩니다).

참고

구현은 ranges::uninitialized_copy_n의 효율성을 향상시킬 수 있습니다 (예를 들어, ranges::copy_n를 사용하여) 만약 출력 범위의 값 타입이 TrivialType인 경우.

기능-테스트 매크로 값 표준 기능
__cpp_lib_parallel_algorithm 202506L (C++26) 병렬 범위 알고리즘
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 위한 특수화된 <memory> 알고리즘, (1)

가능한 구현

struct uninitialized_copy_n_fn
{
    template<std::input_iterator I,
             /*nothrow-input-iterator*/ O, /*nothrow-sentinel-for*/<O> S>
        requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>>
    constexpr ranges::uninitialized_copy_n_result<I, O>
        operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const
    {
        auto iter = std::counted_iterator(std::move(ifirst), count);
        auto ret = ranges::uninitialized_copy(iter, std::default_sentinel, ofirst, olast);
        return {std::move(ret.in).base(), ret.out};
    }
};

inline constexpr uninitialized_copy_n_fn uninitialized_copy_n{};

예제

#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
int main()
{
    const char* stars[]{"Procyon", "Spica", "Pollux", "Deneb", "Polaris"};
    constexpr int n{4};
    alignas(alignof(std::string)) char out[n * sizeof(std::string)];
    try
    {
        auto first{reinterpret_cast<std::string*>(out)};
        auto last{first + n};
        auto ret{std::ranges::uninitialized_copy_n(std::begin(stars), n, first, last)};
        std::cout << '{';
        for (auto it{first}; it != ret.out; ++it)
            std::cout << (it == first ? "" : ", ") << std::quoted(*it);
        std::cout << "};\n";
        std::ranges::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "uninitialized_copy_n exception\n";
    }
}

출력:

{"Procyon", "Spica", "Pollux", "Deneb"};

참고

객체의 범위를 초기화되지 않은 메모리 영역에 복사합니다
(알고리즘 함수 객체)
객체 여러 개를 초기화되지 않은 메모리 영역에 복사합니다
(함수 템플릿)