Namespaces
Variants

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

ja.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以降)
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 );
(C++26以降constexpr) (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

このページで説明されている関数のようなエンティティはアルゴリズム関数オブジェクト (非公式に niebloid として知られています) であり、つまり:

パラメータ

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"};

関連項目

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