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 ) と重なる場合、動作は未定義です。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

パラメータ

ifirst - コピー元要素の 範囲 の先頭
count - コピーする要素数
ofirst, olast - コピー先要素の 範囲 を定義するイテレータ-番兵ペア

戻り値

上記の通り。

計算量

𝓞(N) .

例外

宛先範囲内の要素の構築中にスローされる例外。

注記

実装は、出力範囲の値型が TrivialType である場合、 ranges::copy_n などを使用することで、 ranges::uninitialized_copy_n の効率を改善することができる。

機能テスト マクロ 標準 機能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 対応の 特殊化メモリアルゴリズム , ( 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"};

関連項目

オブジェクトの範囲を未初期化メモリ領域にコピーする
(アルゴリズム関数オブジェクト)
指定された数のオブジェクトを未初期化メモリ領域にコピーする
(関数テンプレート)