In C++, algorithms such as std::accumulate and std::transform are meant to apply such operations. However, there is no simple way of composing those. This can be achieved with Boost.Range, which offers all STL algorithms and adaptors to chain them:

Performance

The question arising naturally is whether this abstraction incurs extra overhead; or whether the Boost library and the STL implementation getting called under the hood invoke some magic that will win the day. In the following snippet, three routines are applied to a std::vector<long long>; a naive loop accessing the elements by index, a C++11 range-based for and the algorithm composition from Boost range: