Boost logo

Boost :

From: Michael Goldshteyn (mgoldshteyn_at_[hidden])
Date: 2006-01-31 12:44:30


"Marcin Kalicinski" <kalita_at_[hidden]> wrote in message
news:drmai0$4k0$1_at_sea.gmane.org...
>> MyMap mymap; // A map of some key type to some value type
>> MyVector myvector; // A vector of elements either of key type or value
>
> (...)
>
>> // Implementation using boost::bind
>> transform(mymap.begin(),mymap.end(),back_inserter
>>(myvector),boost::bind(&MyMap::value_type::second,_1));
>
> And here's the most straighforward of them, but overlooked, implementation
> using BOOST_FOREACH, no std-omitting was necessary at all:
>
> BOOST_FOREACH(const MyMap::value_type &v, mymap)
> myvector.push_back(v.first);
>
> cheers,
> Marcin

An interesting alternate solution, but I was mainly looking for a way that
didn't involve directly using a for loop or a derivative thereof in my code.
That is to say, I was trying to just use a standard algorithm (i.e.
transform), rather than coding a loop, which despite appearances is what
BOOST_FOREACH is. Note that I am not arguing which way is better, only
presenting a solution to those who like for_each and transform.

I have to say, though, that BOOST_FOREACH does have its uses and certainly
allows for a more concise and less error prone way to do something with each
object in a container.

The best solution to the problem is to have standard copy_keys and
copy_values algorithms which, similar to transform takes begin and end
iterators to the source and destination ranges, performing the necessary
.first/.second accesses and then just a straight copy. In fact, I think this
whole ordeal has inspired me enough to perhaps add these to the boost wiki
site.

Michael Goldshteyn


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk