Iteration of items

Many of the routines in <algorithm> will take ranges and iterate over those ranges performing some action. As the name suggests, the fill function will fill a container with a value. The function takes two iterators to specify the range and a value that will be placed into each position of the container:

    vector<int> vec;     vec.resize(5);     fill(vec.begin(), vec.end(), 42);

Since the fill function will be called for a range, it means that you have to pass iterators to a container that already has values, and this is the reason why this code calls the resize method. This code will put the value of 42 into each of the items of the container, so when it has completed the vector contains {42,42,42,42,42}. There is another version ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.