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

std::fill

Defined in header <algorithm>

?

?

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

(1)

?

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

(2)

(since C++17)

1%29分配给value范围内的元素[first, last)...

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

参数

first, last

-

the range of elements to modify

value

-

the value to be assigned

policy

-

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

类型要求

---。

返回值

%280%29

复杂性

一点儿没错last - first任务。

例外

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

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

可能的实施

模板<类前进,类T>空填充%28 Forwardit First,Forwardit Lest,Const T&value%29{for%28;First%21=Lest;++First%29{%2A

*。

下面的代码使用fill()将整数向量的所有元素设置为-1:

二次

代码语言:javascript
复制
#include <algorithm>
#include <vector>
#include <iostream>
 
int main()
{
    std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 
    std::fill(v.begin(), v.end(), -1);
 
    for (auto elem : v) {
        std::cout << elem << " ";
    }
    std::cout << "\n";
}

二次

产出:

二次

代码语言:javascript
复制
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

二次

另见

fill_n

copy-assigns the given value to N elements in a range (function template)

copycopy_if (C++11)

copies a range of elements to a new location (function template)

generate

assigns the results of successive function calls to every element in a range (function template)

transform

applies a function to a range of elements (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com