Namespaces
Variants

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

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
헤더 파일에 정의됨 <memory>
호출 시그니처
template < std:: input_iterator I,

no-throw-input-iterator O, no - throw - 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 < class I, class O >
using uninitialized_copy_n_result = ranges:: in_out_result < I, O > ;
(2) (C++20부터)

N ranges:: min ( count, ranges:: distance ( ofirst, olast ) ) 라고 하자.

N 개의 요소를 ifirst 로 시작하는 범위에서 초기화되지 않은 메모리 영역 [ ofirst , olast ) 로 복사합니다. 마치 다음과 같이 수행하는 것처럼: 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 ) 와 겹치는 경우, 동작은 정의되지 않습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (비공식적으로 niebloids 로 알려진) 즉:

목차

매개변수

ifirst - 복사할 요소들의 범위 의 시작점
count - 복사할 요소의 개수
ofirst, olast - 대상 요소들의 범위 를 정의하는 반복자-감시자 쌍

반환값

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

복잡도

𝓞(N) .

예외

대상 범위의 요소 생성 과정에서 발생하는 모든 예외.

참고 사항

구현체는 출력 범위의 값 타입이 TrivialType 인 경우, 예를 들어 ranges::copy_n 을 사용함으로써 ranges::uninitialized_copy_n 의 효율을 향상시킬 수 있습니다.

Feature-test 매크로 표준 기능
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr for 특수화된 메모리 알고리즘 , ( 1 )

가능한 구현

struct uninitialized_copy_n_fn
{
    template<std::input_iterator I, no-throw-input-iterator O, no-throw-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"};

참고 항목

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