std::generate_n
Материал из cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Заголовочный файл <algorithm>
|
||
template< class OutputIt, class Size, class Generator > void 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.
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:
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.
You can help to correct and verify the translation. Click here for instructions.
Iterator за последним элементом присваивается, если
count>0
, first
иначе. (начиная с 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.
You can help to correct and verify the translation. Click here for instructions.
[править] Сложность
Именно
count
вызовы g()
и заданий, для 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.
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.
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"; }
Вывод:
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. (шаблон функции) | |
сохраняет результат функции в диапазоне 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. (шаблон функции) |