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.
std::fill(v.begin(), v.end(), 7); std::generate(v.begin(), v.end(), [n = 0]() mutable { return n++; });
Try it
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.
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
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)orassign(n, value). - Order of calls:
generatecallsgonce 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
mutablelambda with an init-capture (as above) or capture by reference to share state. - Counting sequences:
std::iotafills 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::fillon trivial types compiles to the same code asmemsetwhen it can. For acharbuffer of zeros,std::string(n, '\0')is clearer. vector<bool>:fillworks but goes through proxy references and is slower than the dedicated member functions.- Ranges (C++20):
std::ranges::fill,fill_n,generateandgenerate_ntake a range. - Complexity: exactly
last - firstassignments.
Related
std::iota,std::copy,std::transform,std::vector- Lessons: Modifying algorithms, Lambdas