C++ std::generate_n() Function

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

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.

C++
std::generate_n(v.begin(), 3, gen);
std::generate_n(std::back_inserter(v), 6, gen);
CallWhat 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
C++
template<class OutputIt, class Size, class Generator>
OutputIt generate_n(OutputIt first, Size count, Generator g);

Parameters

ParameterDescription
firstRequired. Where to start writing.
countRequired. How many values to generate.
gRequired. A function or lambda with no parameters.

Return Value

Return ValueDescription
IteratorPoints 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.

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

  1. n += 10 adds 10 and returns the new value.
  2. The generator is called three times: 10, 20, 30.
  3. 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.

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

  1. The vector starts empty, so we use std::back_inserter.
  2. Each call returns the current p and then doubles it.
  3. Six calls give 1, 2, 4, 8, 16 and 32.

Example 3 — Make labels

Generate strings such as item1, item2.

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

  1. std::to_string() turns the number into text.
  2. "item" + ... joins the word and the number into a std::string.
  3. 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

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.

19 people found this page helpful