std::generate_n

来自cppreference.com

 
 
算法库
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
修改序列操作
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
all_of
any_of
none_of
(C++11)
(C++11)
(C++11)
for_each
count
count_if
mismatch
equal
修改序列操作
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
分区操作
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
is_partitioned(C++11)
partition
partition_copy(C++11)
排序操作(排序的区间)
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
is_sorted(C++11)
is_sorted_until(C++11)
sort
二进制搜索操作(排序的区间)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
设置操作(排序的区间)
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
堆的操作
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最小/最大操作
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
数字操作
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
C库
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template< class OutputIt, class Size, class Generator >

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

OutputIt generate_n( OutputIt first, Size count, Generator g );
(至 C++11)

(C++11 起)
受让人的值,所产生的给定的函数对象g,开始的范围内,在第一count元素first,如果count>0。什么都不做,否则.....
Original:
Assigns values, generated by given function object g, to the first count elements in the range beginning at first, if count>0. Does nothing otherwise.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目录

[编辑] 参数

first -
的范围内的元素,以产生的开始
Original:
the beginning of the range of elements to generate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
count -
的元素,以产生数目
Original:
number of the elements to generate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
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. ​

Type requirements
-
OutputIt must meet the requirements of OutputIterator.

[编辑] 返回值

(无)(至 C++11)
Original:
(none) (至 C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
如果count>0first否则分配的最后一个元素的迭代器一个过去。 (C++11 起)
Original:
Iterator one past the last element assigned if count>0, first otherwise. (C++11 起)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 复杂性

究竟countg()调用和任务,count>0.
Original:
Exactly count invocations of g() and assignments, for count>0.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 可能的实现

template< class OutputIt, class Size, class Generator >
OutputIt generate_n( OutputIt first, Size count, Generator g )
{
    for( Size i = 0; i < count; i++ ) {
        *first++ = g();
    }
    return first;
}

[编辑] 为例

下面的代码填充随机数的整数数组.
Original:
The following code fills an array of integers with random numbers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#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";
}

Output:

52894 15984720 41513563 41346135 51451456

[编辑] 另请参阅

分配一个值,该值到的元素数
Original:
assigns a value to a number of elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函数模板) [edit]
并将结果保存在一个范围内的功能
Original:
saves the result of a function in a range
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(函数模板) [edit]