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

std::uninitialized_fill

Defined in header <memory>

template< class ForwardIt, class T > void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value );

(1)

template< class ExecutionPolicy, class ForwardIt, class T > void uninitialized_fill( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value );

(2)

(since C++17)

1%份29份value到未初始化的内存区域,该区域由范围定义。[first, last)好像

二次

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

二次

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

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

参数

first, last

-

the range of the elements to initialize

value

-

the value to construct the elements with

policy

-

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

类型要求

---。

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

返回值

%280%29

复杂性

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

例外

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

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

可能的实施

模板<类向前,类T>空未初始化[医]填充%28 Forwardit First,Forwardit Lest,Const T&value%29{tyduf type Name std::iterator[医]性状<ForwardIt>*价值[医]类型值;前进当前=第一;尝试{表示%28;当前%21=最后;++当前%29{::新的%28静态[医]铸造<空隙%2A>%28 std::地址%28%2A当前%29%29%29值%28值%29}捕获%28%29%29{表示%28;第一%21=当前;++第29%{第一->值%28%29;}丢弃;}}

*。

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
 
int main()
{
    std::string* p;
    std::size_t sz;
    std::tie(p, sz) = std::get_temporary_buffer<std::string>(4);
 
    std::uninitialized_fill(p, p+sz, "Example");
 
    for (std::string* i = p; i != p+sz; ++i) {
        std::cout << *i << '\n';
        i->~basic_string<char>();
    }
    std::return_temporary_buffer(p);
}

二次

产出:

二次

代码语言:javascript
复制
Example
Example
Example
Example

二次

另见

uninitialized_fill_n

copies an object to an uninitialized area of memory, defined by a start and a count (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券