Algorithms
The Standard Library has an extensive collection of generic functions in the <algorithm>
header file. By generic we mean that they access data via iterators without knowing what the iterators refer to and so it means that you can write generic code to work for any appropriate container. However, if you know the container type and that container has a member method to perform the same action, you should use the member.
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...