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

std::generate_n

Defined in header <algorithm>

?

?

?

(1)

?

template< class OutputIt, class Size, class Generator > void generate_n( OutputIt first, Size count, Generator g );

(until C++11)

template< class ForwardIt, class Size, class Generator > ForwardIt generate_n( ForwardIt first, Size count, Generator g );

(since C++11)

template< class ExecutionPolicy, class OutputIt, class Size, class Generator > OutputIt generate_n( ExecutionPolicy&& policy, OutputIt first, Size count, Generator g );

(2)

(since C++17)

1%29赋值,由给定的函数对象生成。g,第一次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 generate

count

-

number of the elements to generate

policy

-

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

g

-

generator function object that will be called. The signature of the function should be equivalent to the following: Ret fun(); The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. ?

Ret fun();

?

?

Ret fun();

?

?

类型要求

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

---。

返回值

(none)

(until C++11)

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

(since C++11)

复杂性

一点儿没错count调用g()和任务count>0...

例外

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

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

可能的实施

模板<类输出它,类大小,类生成器>输出它生成[医]n%28输出第一,大小计数,生成器g%29{对于%28大小i=0;i<计数;i++%29{%2A第一++=g%28%29;}先返回;}

*。

下面的代码用随机数填充整数数组。

二次

代码语言:javascript
复制
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <algorithm>
 
int main()
{
    const std::size_t N = 5;
    int ar[N];
    std::generate_n(ar, N, std::rand); // Using the C function rand()
 
    std::cout << "ar: ";
    std::copy(ar, ar+N, std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\n";
}

二次

可能的产出:

二次

代码语言:javascript
复制
ar: 52894 15984720 41513563 41346135 51451456

二次

另见

fill_n

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

generate

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

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com