Before You Start
We know that the C++ standard library provides a fill function, which can set every item in a collection to a given value. Let's imagine implementing it ourselves!
Here's how I would implement it…
void fill(vector<int>::iterator startingPoint, vector<int>::iterator pastTheEnd, int val) {
for (vector<int>::iterator iter = startingPoint; iter != pastTheEnd; ++iter) {
*iter = val;
}
}
I used
auto, is that okay?
Yes, that's fine. In fact, it's a good idea to use
autoin this kind of scenario, because it makes your code more robust to changes in the type of the iterator. For example, if you later decide to change the type of the vector tolist<int>, you won't need to change the type of the iterator in theforloop.
Meh. I think it's tedious that you had to write it out anywhere. It'd be cooler if the compiler could just figure it out for you everywhere.
Well…
That's what today's lesson is about! At least kinda…
(When logged in, completion status appears here.)