Composing useful algorithms from standard algorithms - gather
A very nice example for the composability of STL algorithms is gather
. Sean Parent, principal scientist at Adobe Systems at the time, popularized this algorithm because it is both useful and short. The way it is implemented, it is the ideal poster child for the idea of STL algorithm composition.
The gather
algorithm operates on ranges of arbitrary item types. It modifies the order of the items in such a way that specific items are gathered around a specific position, chosen by the caller.
How to do it...
In this section, we will implement the gather
algorithm and a bonus variation of it. Afterward, we see how it can be put to use:
- First, we add all the STL include statements. Then, we declare that we use the
std
namespace:
#include <iostream> #include <algorithm> #include <string> #include <functional> using namespace std;
- The
gather
algorithm is a nice example of standard...