std::uninitialized_copy

定义于头文件 <memory>
template< class InputIt, class ForwardIt >
ForwardIt uninitialized_copy( InputIt first, InputIt last, ForwardIt d_first );
(1)
template< class ExecutionPolicy, class InputIt, class ForwardIt >
ForwardIt uninitialized_copy( ExecutionPolicy&& policy, InputIt first, InputIt last, ForwardIt d_first );
(2) (C++17 起)
1) 复制来自范围 [first, last) 的元素到始于 d_first 的未初始化内存,如同用
for (; first != last; ++d_first, (void) ++first)
   ::new (static_cast<void*>(std::addressof(*d_first)))
      typename std::iterator_traits<ForwardIt>::value_type(*first);
若初始化中抛异常,则函数无效果。
2)(1) ,但按照 policy 执行。此重载不参与重载决议,除非 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 。

目录

参数

first, last - 要复制的元素范围
d_first - 目标范围的起始
policy - 所用的执行策略。细节见执行策略
类型要求
-
InputIt 必须满足 InputIterator 的要求。
-
ForwardIt 必须满足 ForwardIterator 的要求。
-
通过ForwardIt 合法实例的自增、赋值、比较或间接均不可抛异常。

返回值

指向最后复制的元素后一元素的迭代器。

复杂度

firstlast 间的距离成线性

异常

拥有名为 ExecutionPolicy 的模板参数的重载按下列方式报告错误:

可能的实现

template<class InputIt, class ForwardIt>
ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt d_first)
{
    typedef typename std::iterator_traits<ForwardIt>::value_type Value;
    ForwardIt current = d_first;
    try {
        for (; first != last; ++first, (void) ++current) {
            ::new (static_cast<void*>(std::addressof(*current))) Value(*first);
        }
        return current;
    } catch (...) {
        for (; d_first != current; ++d_first) {
            d_first->~Value();
        }
        throw;
    }
}

示例

#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(v.begin(), v.begin() + sz, p);
 
    for (std::string* i = p; i != p+sz; ++i) {
        std::cout << *i << ' ';
        i->~basic_string<char>();
    }
    std::return_temporary_buffer(p);
}

输出:

This is an example

参阅

将指定数量的对象复制到未初始化的内存区域
(函数模板)

版本历史

  • (当前 | 先前 2017年9月11日 (一) 08:44Fruderica讨论 | 贡献. . (2,897字节) (+898). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:57P12bot讨论 | 贡献 . . (1,999字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:07P12bot讨论 | 贡献 . . (1,999字节) (-84). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:33P12bot讨论 | 贡献 . . (2,083字节) (+297). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:41P12讨论 | 贡献 . . (1,786字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,786字节) (-61). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:55P12讨论 | 贡献 . . (1,847字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,847字节) (+1,847). . (Translated from the English version using Google Translate)