名稱空間
變體
操作

std::uninitialized_copy_n

來自 cppreference.com
< cpp‎ | 記憶體
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
uninitialized_copy_n
(C++11)
(C++17)
受約束的未初始化
記憶體演算法
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>
template< class InputIt, class Size, class NoThrowForwardIt >

NoThrowForwardIt uninitialized_copy_n( InputIt first, Size count,

                                       NoThrowForwardIt d_first );
(1) (C++11 起)
(C++26 起為 constexpr)
template< class ExecutionPolicy, class ForwardIt,

          class Size, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_copy_n( ExecutionPolicy&& policy,
                                       ForwardIt first, Size count,

                                       NoThrowForwardIt d_first );
(2) (C++17 起)
1) 從起始於 first 的範圍複製 count 個元素到起始於 d_first 的未初始化記憶體區域,如同透過

for (; count > 0; ++d_first, (void) ++first, --count)
    ::new (voidify(*d_first))
        typename std::iterator_traits<NoThrowForwardIt>::value_type(*first);

如果在初始化期間丟擲異常,則已構造的物件將以未指定順序銷燬。
2)(1),但根據 policy 執行。
僅當滿足以下所有條件時,此過載才參與過載決議

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(C++20 起)


d_first + [0count)first + [0count) 重疊,則行為未定義。

(C++20 起)

目錄

[編輯] 引數

first - 要複製的元素範圍的起始
count - 要複製的元素數量
d_first - 目標範圍的開頭
policy - 要使用的 執行策略
型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。
-
ForwardIt 必須滿足 LegacyForwardIterator 的要求。
-
NoThrowForwardIt 必須滿足 LegacyForwardIterator 的要求。
-
透過 `NoThrowForwardIt` 的有效例項,不允許遞增、賦值、比較或間接引用丟擲異常。

[編輯] 返回值

指向最後一個被複制元素之後的元素的迭代器。

[編輯] 複雜度

關於 count 的線性複雜度。

[編輯] 異常

帶有名為 ExecutionPolicy 的模板引數的過載會按如下方式報告錯誤:

  • 若作為演算法一部分呼叫的函式執行丟擲異常且 ExecutionPolicy標準策略之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為是實現定義的。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 註解

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

[編輯] 可能的實現

template<class InputIt, class Size, class NoThrowForwardIt>
constexpr NoThrowForwardIt uninitialized_copy_n(InputIt first, Size count,
                                                NoThrowForwardIt d_first)
{
    using T = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = d_first;
    try
    {
        for (; count > 0; ++first, (void) ++current, --count)
            ::new (static_cast<void*>(std::addressof(*current))) T(*first);
    }
    catch (...)
    {
        for (; d_first != current; ++d_first)
            d_first->~T();
        throw;
    }
    return current;
}

[編輯] 示例

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
 
int main()
{
    std::vector<std::string> v = {"This", "is", "an", "example"};
 
    std::string* p;
    std::size_t sz;
    std::tie(p, sz) = std::get_temporary_buffer<std::string>(v.size());
    sz = std::min(sz, v.size());
 
    std::uninitialized_copy_n(v.begin(), sz, p);
 
    for (std::string* i = p; i != p + sz; ++i)
    {
        std::cout << *i << ' ';
        i->~basic_string<char>();
    }
    std::cout << '\n';
 
    std::return_temporary_buffer(p);
}

可能的輸出

This is an example

[編輯] 缺陷報告

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

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2133 C++98 效果描述使用了帶有迭代
表示式 ++d_first, ++first, --countfor 迴圈,它
導致 operator, 的實參依賴查詢
丟棄了一個運算元的
值以
停用那些 ADL
LWG 2433 C++11 此演算法可能被過載的 operator& 劫持 使用 std::addressof
LWG 3870 C++20 此演算法可能在 const 儲存上建立物件 保持不允許

[編輯] 參閱

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