MORE ON ALGORITHMS

The algorithm and numeric headers define a large number of algorithms. The algorithms in the numeric header are primarily devoted to processing arrays of numerical values, whereas those in the algorithm header are more general purpose and provide such things as the ability to search, sort, copy, and merge sequences of objects specified by iterators. There are far too many to discuss in detail in this introductory chapter, so I’ll just introduce a few of the most useful algorithms from the algorithm header to give you a basic idea of how they can be used.

You have already seen the sort() and copy() algorithms from the algorithm header in action. Let’s take a brief look at a few other of the more interesting functions in the algorithm header:

The fill() algorithm

The fill() algorithm is of this form:
fill(ForwardIterator begin, ForwardIterator end, const Type& value)
This fills the elements specified by the iterators begin and end with value. For example, given a vector v, storing values of type string containing more than 10 elements, you could write:
fill(v.begin(), v.begin()+10, "invalid");
This would set the first 10 elements in v to "invalid", the value specified by the last argument to fill().

The replace() algorithm

The replace() algorithm is of the form:
replace(ForwardIterator begin, ForwardIterator end,
                                      const Type& oldValue, const Type& newValue)
This function examines each element in the range specified by begin and end, and replaces each occurrence ...

Get Ivor Horton's Beginning Visual C++ 2012 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.