which is relatively straightforward, but push_back_to_vector_if_color_is_bright() will hardly ever be reused.
- it is tightly coupled to std::pair<int,Shape*>
- it is tightly coupled to std::vector<int>

If we choose to follow this path, the best that can be done make the for_each more readable, is to write a function object:

Exploring alternative ways to express

I think that since copying is involved, a std::copy_if() would be ideal to express it. However, there is a transformation involved here (from pair<int,Shape*> to Shape* then to color), so copy_if is not easy to implement.

As a second best, std::transform() could be used. I could either

transform map value_types to Shapes, and then insert to the vector if the color of the shape is bright

transform map value_types to Shapes to colors, and then insert to the vector if color is bright

select2nd(), which is SGI's implementation that selects the second from a pair (hence the value from a map). A select1st() also exists, to select the key from a map.

conditional_back_inserter(), which we'll have to implement. If we do so, we should also provide a conditional_inserter, and a conditional_front_inserter, for completeness in our library. The downside of this is that this is custom code and has no value outside our codebase.

transformation iterators

It would be nice if the iterators were clever enough to do the transformation themselves. Then a copy_if algorithm could be put to use:

copy_if (begin_map_iterator_to_color, end_map_iterator_to_color,

back_inserter(colors),

color_is_bright );

I recently discovered that boost has a number of flexible iterators. Here's what you could do with a boost::transform_iterator:

Concluding

Although the example does not fit all cases you might face, you can see that there is some choice in expression.

First, you can choose on how to name your algorithms. for_each is more technical, less expressive. transform still involves some consideration that the container is a map. copy_if feels closer to what we want to achieve.