名稱空間
變體
操作

std::ranges::uninitialized_copy, std::ranges::uninitialized_copy_result

來自 cppreference.com
< cpp‎ | 記憶體
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
(C++17)
受約束的未初始化
記憶體演算法
ranges::uninitialized_copy
(C++20)
C 庫

分配器
記憶體資源
垃圾回收支援
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化儲存
(直到 C++20*)
(直到 C++20*)
顯式生命週期管理
 
定義於標頭檔案 <memory>
呼叫簽名 (Call signature)
template< std::input_iterator I, std::sentinel_for<I> S1,
          no-throw-forward-iterator O, no-throw-sentinel-for<O> S2 >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_reference_t<I>>
uninitialized_copy_result<I, O>
    uninitialized_copy( I ifirst, S1 ilast, O ofirst, S2 olast );
(1) (C++20 起)
(C++26 起為 constexpr)
template< ranges::input_range IR, no-throw-forward-range OR >
    requires std::constructible_from<ranges::range_value_t<OR>,
                                     ranges::range_reference_t<IR>>
uninitialized_copy_result<ranges::borrowed_iterator_t<IR>,
                          ranges::borrowed_iterator_t<OR>>
    uninitialized_copy( IR&& in_range, OR&& out_range );
(2) (C++20 起)
(C++26 起為 constexpr)
輔助型別
template< class I, class O >
using uninitialized_copy_result = ranges::in_out_result<I, O>;
(3) (C++20 起)

Nranges::min(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast))

1) 將範圍 [ifirstilast) 中的 N 個元素構造到未初始化記憶體區域 [ofirstolast),如同:

for (; ifirst != ilast && ofirst != olast; ++ofirst, (void)++ifirst)
    ::new (voidify(*ofirst)) std::remove_reference_t<std::iter_reference_t<O>>(*ifirst);
return {std::move(ifirst), ofirst};

如果在初始化期間丟擲異常,則已構造的物件將以未指定順序銷燬。
如果 [ofirstolast)[ifirstilast) 重疊,則行為未定義。
2) 等價於 return ranges::uninitialized_copy(ranges::begin(in_range), ranges::end(in_range),
                                  ranges::begin(out_range), ranges::end(out_range));

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

ifirst, ilast - 定義要複製的源元素範圍的迭代器-哨兵對
in_range - 要複製的源元素range
ofirst, olast - 定義目標元素範圍的迭代器-哨兵對
out_range - 目標range

[編輯] 返回值

如上所述。

[編輯] 複雜度

𝓞(N)

[編輯] 異常

在目標範圍內構造元素時丟擲的任何異常。

[編輯] 注意

如果輸出範圍的值型別是 TrivialType,則實現可以提高 ranges::uninitialized_copy 的效率。

特性測試 標準 特性
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用於未初始化記憶體演算法(1,2)

[編輯] 可能的實現

struct uninitialized_copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S1,
             no-throw-forward-iterator O, no-throw-sentinel-for<O> S2>
        requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>>
    constexpr ranges::uninitialized_copy_result<I, O>
        operator()(I ifirst, S1 ilast, O ofirst, S2 olast) const
    {
        O current{ofirst};
        try
        {
            for (; !(ifirst == ilast or current == olast); ++ifirst, ++current)
                ranges::construct_at(std::addressof(*current), *ifirst);
            return {std::move(ifirst), std::move(current)};
        }
        catch (...) // rollback: destroy constructed elements
        {
            for (; ofirst != current; ++ofirst)
                ranges::destroy_at(std::addressof(*ofirst));
            throw;
        }
    }
 
    template<ranges::input_range IR, no-throw-forward-range OR>
        requires std::constructible_from<ranges::range_value_t<OR>,
    constexpr ranges::range_reference_t<IR>>
        ranges::uninitialized_copy_result<ranges::borrowed_iterator_t<IR>,
                                          ranges::borrowed_iterator_t<OR>>
    operator()(IR&& in_range, OR&& out_range) const
    {
        return (*this)(ranges::begin(in_range), ranges::end(in_range),
                       ranges::begin(out_range), ranges::end(out_range));
    }
};
 
inline constexpr uninitialized_copy_fn uninitialized_copy{};

[編輯] 示例

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
 
int main()
{
    const char* v[]{"This", "is", "an", "example"};
 
    if (const auto sz{std::size(v)};
        void* pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz))
    {
        try
        {
            auto first{static_cast<std::string*>(pbuf)};
            auto last{first + sz};
            std::ranges::uninitialized_copy(std::begin(v), std::end(v), first, last);
 
            std::cout << "{";
            for (auto it{first}; it != last; ++it)
                std::cout << (it == first ? "" : ", ") << std::quoted(*it);
            std::cout << "};\n";
 
            std::ranges::destroy(first, last);
        }
        catch (...)
        {
            std::cout << "uninitialized_copy exception\n";
        }
        std::free(pbuf);
    }
}

輸出

{"This", "is", "an", "example"};

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3870 C++20 此演算法可能在 const 儲存上建立物件 保持不允許

[編輯] 參閱

將一定數量的物件複製到未初始化記憶體區域
(演算法函式物件)[編輯]
將物件範圍複製到未初始化記憶體區域
(函式模板) [編輯]