C++ std::generate_n() Function

Definition
In C++, std::generate_n() calls a generator function n times and writes each result to the destination, starting at an iterator.
Paired with std::back_inserter, it is a handy way to build a new vector of generated values.
Syntax
Add #include <algorithm>, then call std::generate_n(). Start with these two lines.
std::generate_n(v.begin(), 3, gen);
std::generate_n(std::back_inserter(v), 6, gen);
| Call | What it does |
|---|
std::generate_n(v.begin(), 3, gen); | Fill the first three elements. |
std::generate_n(std::back_inserter(v), 6, gen); | Append six generated values. |
Library declarations
template<class OutputIt, class Size, class Generator>
OutputIt generate_n(OutputIt first, Size count, Generator g);
Parameters
| Parameter | Description |
|---|
| first | Required. Where to start writing. |
| count | Required. How many values to generate. |
| g | Required. A function or lambda with no parameters. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points just after the last element written. |
C++ Examples
Example 1 — Fill the first three
Generate only a few values at the start of a vector.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v(4, 0);
int n = 0;
std::generate_n(v.begin(), 3, [&n]() { return n += 10; });
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
n += 10 adds 10 and returns the new value.- The generator is called three times: 10, 20, 30.
- The fourth element is not part of the count, so it stays 0.
Example 2 — Powers of two
Build a new vector with doubling numbers.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v;
int p = 1;
std::generate_n(std::back_inserter(v), 6, [&p]() {
int value = p;
p *= 2;
return value;
});
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
- The vector starts empty, so we use
std::back_inserter. - Each call returns the current
p and then doubles it. - Six calls give 1, 2, 4, 8, 16 and 32.
Example 3 — Make labels
Generate strings such as item1, item2.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main() {
std::vector<std::string> names;
int id = 1;
std::generate_n(std::back_inserter(names), 3, [&id]() {
return "item" + std::to_string(id++);
});
for (const std::string& x : names) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
How It Works
std::to_string() turns the number into text."item" + ... joins the word and the number into a std::string.id++ makes each label different.
Frequently Asked Questions
In C++, std::generate_n() fills n elements 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 an iterator to the element after the last one that was written.
generate() takes two iterators. generate_n() takes one iterator and a count.
Use std::back_inserter as the first argument. The vector grows by one element for each generated value.
Next: includes()
Checks whether a sorted range contains all elements of another sorted range.
includes() tutorial →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.
19 people found this page helpful