首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::uninitialized_copy

Defined in header <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)

(since C++17)

1%个来自范围内的29个拷贝元素[first, last)开始的未初始化内存区域。d_first好像

二次

代码语言:javascript
复制
for (; first != last; ++d_first, (void) ++first)
   ::new (static_cast<void*>(std::addressof(*d_first)))
      typename std::iterator_traits<ForwardIt>::value_type(*first);

二次

如果在初始化期间抛出异常,则该函数没有任何效果。

2%29与%281%29相同,但根据policy此重载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

the range of the elements to copy

d_first

-

the beginning of the destination range

policy

-

the execution policy to use. See execution policy for details.

类型要求

-输入必须符合输入器的要求。

---。

-不通过有效的Forward实例进行增量、赋值、比较或间接转换,否则会引发异常。

返回值

Iterator到元素的最后一个复制元素。

复杂性

直线在之间的距离firstlast...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

模板<类输入,类向前>未初始化[医]复制%28 InputIt First,InputIt Lest,Forwardit d[医]第一%29{tydurif type Name std::iterator[医]性状<ForwardIt>*价值[医]类型值;正向电流=d[医]首先,尝试{表示%28;第一%21=最后;++第一,%28空%29++当前%29{::新的%28静态[医]铸造<空隙%2A>%28 std::地址%28%2A现值%29%29%29值%28%2A第一%29;}返回电流;}捕获%28...%29{%28;d[医]第一%21=电流;++d[医]第一%29{d[医]第一个->~值%28%29;}抛出;}}

*。

二次

代码语言:javascript
复制
#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);
}

二次

产出:

二次

代码语言:javascript
复制
This is an example

二次

另见

uninitialized_copy_n (C++11)

copies a number of objects to an uninitialized area of memory (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券