Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Algorithms

The std::fill and std::generate algorithms

Reference for C++ std::fill, fill_n, generate and generate_n: set every element to a value or to the result of a function, with ranges versions.

std::fill assigns the same value to every element of a range. std::generate calls a function with no arguments once per element and assigns each result, which is how you fill a range with a counter, random numbers or computed values. Each has an _n variant that works on the first n elements starting at an iterator.

These algorithms assign to existing elements; they do not change a container's size. To create a vector of ten zeros use the constructor std::vector<int>(10, 0) or assign, not fill on an empty vector.

C++
std::fill(v.begin(), v.end(), 7);
std::generate(v.begin(), v.end(), [n = 0]() mutable { return n++; });

Try it

C++ fill and fill_n

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ generate with state

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class FwdIt, class T>
void fill(FwdIt first, FwdIt last, const T& value);

template<class OutIt, class Size, class T>
OutIt fill_n(OutIt first, Size count, const T& value);

template<class FwdIt, class Gen>
void generate(FwdIt first, FwdIt last, Gen g);

template<class OutIt, class Size, class Gen>
OutIt generate_n(OutIt first, Size count, Gen g);
Name Description
first, last The range to overwrite.
count Number of elements for the _n forms.
value Assigned to each element.
g Callable with no arguments; its result is assigned to each element.

Returns: fill and generate return nothing; fill_n and generate_n return an iterator one past the last element written.

Errors / undefined behavior: writing beyond the end of the range is undefined. A negative count writes nothing.

Usage notes

  • No resizing: the range must already exist. For a new container, use the constructor vector<int>(n, value) or assign(n, value).
  • Order of calls: generate calls g once for each element in order from first to last, so a stateful generator produces a predictable sequence.
  • Stateful generators: the generator is passed by value; use a mutable lambda with an init-capture (as above) or capture by reference to share state.
  • Counting sequences: std::iota fills with increasing values and is the standard choice for 0, 1, 2, ...
  • Random data: std::generate(v.begin(), v.end(), [&] { return dist(engine); }) with a seeded engine. Distributions may give different sequences on different standard libraries, so do not hard-code expected values.
  • Zeroing memory: std::fill on trivial types compiles to the same code as memset when it can. For a char buffer of zeros, std::string(n, '\0') is clearer.
  • vector<bool>: fill works but goes through proxy references and is slower than the dedicated member functions.
  • Ranges (C++20): std::ranges::fill, fill_n, generate and generate_n take a range.
  • Complexity: exactly last - first assignments.