std::generate
Материал из 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 ForwardIt, class Generator > void generate( ForwardIt first, ForwardIt last, Generator g ); |
||
Назначение каждого элемента в диапазоне
[first, last)
стоимости, созданной в данной функции объекта g
. Original:
Assigns each element in range
[first, last)
a value generated by the given function object g
. 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, last | - | диапазон элементов для генерации
Original: 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. | |||||||||
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 ForwardIt can be dereferenced and assigned a value of type Ret. | |||||||||
Type requirements | |||||||||||
-ForwardIt must meet the requirements of ForwardIterator .
|
[править] Возвращаемое значение
(Нет)
Original:
(none)
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.
[править] Сложность
Именно std::distance(first, last) вызовы
g()
и заданий.Original:
Exactly std::distance(first, last) invocations of
g()
and assignments.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 ForwardIt, class Generator> void generate(ForwardIt first, ForwardIt last, Generator g) { while (first != last) { *first++ = g(); } } |
[править] Пример
Следующие виды код заполняет вектор случайных чисел
Original:
The following code uses fills a vector 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 <algorithm> #include <iostream> #include <cstdlib> int main() { std::vector<int> v(5); std::generate(v.begin(), v.end(), std::rand); // Using the C function rand() std::cout << "v: "; for (auto iv: v) { std::cout << iv << " "; } std::cout << "\n"; }
Вывод:
v: 52894 15984720 41513563 41346135 51451456
[править] См. также
назначает диапазон элементов определенного значения Original: assigns a range of elements a certain value The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) | |
сохраняет результат N приложениях функции Original: saves the result of N applications of a function The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (шаблон функции) |