C++ std::generate() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

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.

C++
std::generate(v.begin(), v.end(), gen);
std::generate(v.begin(), v.end(), [&n] { return n++; });
CallWhat 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
C++
template<class ForwardIt, class Generator>
void generate(ForwardIt first, ForwardIt last, Generator g);

Parameters

ParameterDescription
first, lastRequired. The range to fill.
gRequired. A function or lambda with no parameters. Its return value is stored in each element.

Return Value

Return ValueDescription
voidNothing is returned.

C++ Examples

Example 1 — Count up from zero

Fill a vector with 0, 1, 2, 3, 4.

C++
#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

  1. The lambda captures n by reference, so it can change it between calls.
  2. n++ returns the old value, then adds 1.
  3. The five calls return 0, 1, 2, 3 and 4.

Example 2 — Squares

Fill a vector with square numbers.

C++
#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

  1. The lambda multiplies i by itself and stores it as result.
  2. Then it adds 1 to i, so the next call squares the next number.
  3. That gives 1, 4, 9, 16, 25.

Example 3 — A plain function

Pass a normal function instead of a lambda.

C++
#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

  1. roll() has a static variable that keeps its value between calls.
  2. die % 6 + 1 counts from 1 to 6 and then wraps back to 1.
  3. 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.

Next: generate_n()

Fills n elements with values made by calling a function.

generate_n() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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