C++ std::generate() Function

Definition
In C++, std::generate() calls a function once for each element in a range and stores the result in that element.
The function takes no arguments. It can keep its own counter, so each call can return a different value, such as 0, 1, 2, 3.
Syntax
Add #include <algorithm>, then call std::generate(). Start with these two lines.
std::generate(v.begin(), v.end(), gen);
std::generate(v.begin(), v.end(), [&n] { return n++; });
| Call | What it does |
|---|
std::generate(v.begin(), v.end(), gen); | Fill the range with values from gen(). |
std::generate(v.begin(), v.end(), [&n] { return n++; }); | Use a lambda that keeps a counter. |
Library declarations
template<class ForwardIt, class Generator>
void generate(ForwardIt first, ForwardIt last, Generator g);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to fill. |
| g | Required. A function or lambda with no parameters. Its return value is stored in each element. |
Return Value
| Return Value | Description |
|---|
void | Nothing is returned. |
C++ Examples
Example 1 — Count up from zero
Fill a vector with 0, 1, 2, 3, 4.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v(5);
int n = 0;
std::generate(v.begin(), v.end(), [&n]() { return n++; });
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The lambda captures
n by reference, so it can change it between calls. n++ returns the old value, then adds 1.- The five calls return 0, 1, 2, 3 and 4.
Example 2 — Squares
Fill a vector with square numbers.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v(5);
int i = 1;
std::generate(v.begin(), v.end(), [&i]() {
int result = i * i;
++i;
return result;
});
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The lambda multiplies
i by itself and stores it as result. - Then it adds 1 to
i, so the next call squares the next number. - That gives 1, 4, 9, 16, 25.
Example 3 — A plain function
Pass a normal function instead of a lambda.
#include <algorithm>
#include <iostream>
#include <vector>
int roll() {
static int die = 0;
die = die % 6 + 1;
return die;
}
int main() {
std::vector<int> v(8);
std::generate(v.begin(), v.end(), roll);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
roll() has a static variable that keeps its value between calls.die % 6 + 1 counts from 1 to 6 and then wraps back to 1.- Eight calls fill the vector with 1 to 6 and then 1 and 2 again.
Frequently Asked Questions
In C++, std::generate() fills a range with values made by calling a function. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). Each element in the range gets the value returned by the generator.
fill() writes the same value everywhere. generate() calls a function for each element, so every element can be different.
No. It only changes existing elements. Use std::generate_n() with std::back_inserter to add new elements.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
17 people found this page helpful