Motivation
Revisiting my C++ skills after nearly 14 years of abstinence I came across I nice little function called iota
.
It is part of the <numeric>
header and is used to fill a container with a sequence of numbers.
The function is overloaded for different types of containers and different types of numbers.
The following example shows how to use it with a std::vector<int>
and a std::int32_t
as start value.
Code
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> numbers(5);
std::int32_t startValue = 10;
std::iota(numbers.begin(), numbers.end(), startValue);
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}