Write-only range algorithms
We began this chapter looking at algorithms such as std::find
that march through a range reading its elements in order without modification. You might be surprised to learn that the inverse operation also makes sense: there is a family of standard algorithms that march through a range modifying each element without reading it!
std::fill(a,b,v)
does what its name implies: fill each element of the given range [a,b)
with a copy of the provided value v
.
std::iota(a,b,v)
is slightly more interesting: it fills the elements of the given range with copies of ++v
. That is, std::iota(a,b,42)
will set a[0]
equal to 42, a[1]
equal to 43, a[2]
equal to 44, and so on all the way up to b
. This algorithm's funny name comes from the APL programming language, where a function named ι
(that's the Greek letter iota) performed this operation. Another funny thing about this algorithm is that, for whatever reason, its definition is found in the standard <numeric>
header instead...