Boost logo

Boost :

From: Marcin Kalicinski (kalita_at_[hidden])
Date: 2006-01-30 19:21:28


> It is often the case that one is dealing with key value pairs when using
> the STL containers. Sometimes, I have found the need to extract either the
> key or the value from these pairs as part of a call to a standard C++
> algorithm. Unfortunatelly, unless one is using the SGI implementation of
> STL, function objects such as select1st and select2nd are not available.
> So, I started to look for a boost equivalent and finally found it in the
> bind library. Here are the mechanisms, with the std prefix omitted for
> brevity:
>
> 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


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