iota
Usage
std::iota
fills a range with some incrementable value. The function is almost like Python’s range
function, but in C++. You can use it like this:
std::vector<int> my_range(10);
std::atoi(my_range.begin(), my_range.end(), 0);
// Now, `my_range` contains 0, 1, 2, .., 9.
The name std::iota
is a bit cryptic. It is the kind of function name that, if used properly, signals you have discovered the zen of C++.
Naming Theory 1
iota
means:
an infinitesimal amount
and is also the Greek letter i
.
std::iota
captures the common pattern using i
:
for (int i = 0; i < c.size(); ++i) {
c[i] = i;
}
Naming Theory 2
std::iota
is a reversal of std::atoi
. In some sense, these functions are mirror imagaes of each other:
std::atoi
parses an array (of ascii characters) into an integer, (unless there’s an error, in which casestd::atoi
just returns 0).std::iota
broadcasts an integer into an array.
Functions like iota
and atoi
remind me of bash
, where we embrace the yin-yang dynamics and karmetic symmetry of the universe with syntax like:
case
esac
if
fi
do
done
(od
was taken)