Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2003-07-13 05:28:50


At 22:51 2003-07-10, Marshall Clow wrote:
>At 7:21 AM -0400 7/10/03, David Abrahams wrote:
>>Marshall Clow <marshall_at_[hidden]> writes:
>>
>> > So, here they are. Are they useful to anyone else? Is there some
>> reason that
>>> they don't already exist? Did I miss them somewhere?
>>>
>>> template <class T1, class T2>
>>> struct first: std::unary_function< std::pair <T1, T2>, T1>
>>> {
>>> T1 operator()(const std::pair <T1, T2> & x) const { return
>>> x.first;}
>>> };
>>>
>>> template <class T1, class T2>
>>> struct first: std::unary_function< std::pair <T1, T2>, T2>
>>> {
>>> T2 operator()(const std::pair <T1, T2> & x) const { return
>>> x.second;}
>>> };
>>>
>But other than that, what do people think?

I would prefer "select_nth".

>I needed them because someone had a map<int, Foo*>, and I wanted to call a
>member function
>(call it "Bar") on each Foo* in the map. Turns out, that with
>boost::compose and 'second', it
>was simple:
>
> std::for_each ( m.begin (), m.end (),
> boost::compose_f_gx ( std::mem_fun ( Foo::Bar ), second
> <int, Foo *> ()));

How about:

std::for_each(m.begin(), m.end(),
   boost::compose_f_gx(std::mem_fun(Foo::Bar), select_nth<1, std::pair<int,
Foo*> >()));

See
http://www.cs.umu.se/~ens01dwn/select_nth.hpp
for a sample implementation that handles boost::tuples and pairs.

---
Daniel Wallin

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