boost equivalents for select1st and select2nd

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

boost equivalents for select1st and select2nd

Michael Goldshteyn
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:

Given:

MyMap mymap; // A map of some key type to some value type
MyVector myvector; // A vector of elements either of key type or value type,
depending on requirements

---

// Copy all keys from the map to the vector, which is assumed to containe
elements of key type:

// Implementation using select1st, when available (e.g. SGI STL extensions)
transform(mymap.begin(),mymap.end(),back_inserter(myvector),select1st<MyMap::value_type>());

// Implementation using boost::bind
transform(mymap.begin(),mymap.end(),back_inserter(myvector),boost::bind(&MyMap::value_type::first,_1));

// Copy all keys from the map to the vector, which, in this case, is assumed
to containe elements of value type:

// Implementation using select2nd, when available
transform(mymap.begin(),mymap.end(),back_inserter(myvector),select2nd<MyMap::value_type>());

// Implementation using boost::bind
transform(mymap.begin(),mymap.end(),back_inserter(myvector),boost::bind(&MyMap::value_type::second,_1));

---

I hope this helps anyone else that needs similar functionality and doesn't
want to reinvent the wheel.

Thanks,

Michael Goldshteyn



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: boost equivalents for select1st and select2nd

Marcin Kalicinski
> 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



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: boost equivalents for select1st and select2nd

Michael Goldshteyn

"Marcin Kalicinski" <[hidden email]> wrote in message
news:drmai0$4k0$[hidden email]...

>> 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



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost