This particular need is met with boost::adapters::keys
. More generally, boost::range
allows you to design your own adapters, but it's somewhat involved. So unless you're designing a new library, you might be able to get away with transformed
.
If you need the result of your range back into a container, you can write a simple collect
function which will "collect" the result of a boost::range
pipeline.
template<typename Output, typename SinglePassRange>
Output collect(const SinglePassRange & rng)
{
Output r;
boost::range::copy(rng, std::inserter(r, boost::begin(r)));
return r;
}
Now you can easily whip up some little functions and
collect<vector<int>>(numbers | filtered(odd))
collect<vector<int>>(numbers | transformed(doubled))
collect<vector<K>>(myMap | transformed(keyOf))