CKTIS / C++
std::iota: fill with successive values
std::iota assigns value to the first element and increments value after each assignment for the next element.
Version: C++23
Syntax
template<class ForwardIt, class T>
constexpr void std::iota(ForwardIt first, ForwardIt last, T value);std::iota(first,last,value) performs *i = value and then ++value for every element. The initial type must be convertible to the iterator value type and pre-increment must be well-formed. The algorithm is constexpr in C++23 and performs exactly as many assignments and increments as there are elements.
The example starts at -2 and fills a five-element array, so it prints -2 -1 0 1 2. The classic function returns nothing; std::ranges::iota differs by returning both the final iterator and final value.
Increment also occurs after assigning the final element. A signed-integer sequence must therefore leave room for that last ++value, because signed overflow is undefined behavior; an unsigned type wraps modulo. For a user type, its increment operator determines meaning and cost. The range must be valid and writable, and an assignment or increment exception propagates after a partially filled prefix.
Examples
#include <array>
#include <iostream>
#include <numeric>
int main() {
std::array<int, 5> values{};
std::iota(values.begin(), values.end(), -2);
for (int value : values) std::cout << value << ' ';
std::cout << '\n';
}
Result
-2 -1 0 1 2
std::iota → -2 -1 0 1 2