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

std::fill_n

Defined in header <algorithm>

?

?

?

(1)

?

template< class OutputIt, class Size, class T > void fill_n( OutputIt first, Size count, const T& value );

(until C++11)

template< class OutputIt, class Size, class T > OutputIt fill_n( OutputIt first, Size count, const T& value );

(since C++11)

template< class ExecutionPolicy, class ForwardIt, class Size, class T > ForwardIt fill_n( ExecutionPolicy&& policy, ForwardIt first, Size count, const T& value );

(2)

(since C++17)

1%29分配给value第一次count范围中的元素开始于first如果count > 0.做其他的事。

2%29与%281%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first

-

the beginning of the range of elements to modify

count

-

number of elements to modify

value

-

the value to be assigned

policy

-

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

类型要求

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

---。

返回值

(none)

(until C++11)

Iterator one past the last element assigned if count > 0, first otherwise.

(since C++11)

复杂性

一点儿没错count任务,为count > 0...

例外

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

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

可能的实施

模板<类输出,类大小,类T>输出填充[医]n%28 OutputIt First,Size Count,Const T&value%29{for%28 Size i=0;i<count;i++%29{%2A首先++=值;}先返回;}

*。

下面的代码使用fill_n()将-1赋给整数向量的前半部分:

二次

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

二次

产出:

二次

代码语言:javascript
复制
-1 -1 -1 -1 -1 5 6 7 8 9

二次

另见

fill

copy-assigns the given value to every element in a range (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com