C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

adapter for .second of pair<> ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Kevin Bailey
Guest





PostPosted: Sun May 08, 2005 5:47 pm    Post subject: adapter for .second of pair<> ? Reply with quote



I'm trying to find a clean solution for making
an adapter which would pull the second (or first)
out of a pair<>. So far I only have this ugly thing
which only works for member functions (what my
friend needed.) Surely there is a more generic
adapter in, say, boost for this ? If so, I
haven't found it.

#include <algorithm>
#include <functional>
#include <map>

struct Whatever
{
int a, b;

void do_something();
};

template <typename T1, typename T2>
struct thesecond:
public std::unary_function<void, T2>
{
const std::mem_fun_t<void, T2>& _uf;

thesecond(
const std::mem_fun_t<void, T2>& uf)
:_uf(uf)
{
}

void operator()(
std::pair<const T1, T2>& t)
{
_uf(&t.second);
}
};

void function(
std::map<int, Whatever>& M)
{
std::for_each(M.begin(), M.end(),
thesecond<int, Whatever>(
std::mem_fun(&Whatever::do_something)));
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Mon May 09, 2005 6:17 am    Post subject: Re: adapter for .second of pair<> ? Reply with quote



In article <ZwMee.7623$BE3.7150 (AT) newsread2 (DOT) news.pas.earthlink.net>,
Kevin Bailey <noone (AT) nowhere (DOT) com> wrote:

Quote:
I'm trying to find a clean solution for making
an adapter which would pull the second (or first)
out of a pair<>. So far I only have this ugly thing
which only works for member functions (what my
friend needed.) Surely there is a more generic
adapter in, say, boost for this ? If so, I
haven't found it.

well std::pair provides typedefs of its arguments so


template <class Pair>
:std::unary_function<typename Pair::second_type,Pair>
struct the_second
{
typename Pair::second_type operator () (const Pair &x) const
{ return x.second;}
};

template <class Pair>
:std::unary_function<typename Pair::first_type,Pair>
struct the_first
{
typename Pair::first_type operator () (const Pair &x) const
{ return x.first;}
}:

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maxim Yegorushkin
Guest





PostPosted: Mon May 09, 2005 12:15 pm    Post subject: Re: adapter for .second of pair<> ? Reply with quote



On Sun, 08 May 2005 21:47:52 +0400, Kevin Bailey <noone (AT) nowhere (DOT) com> wrote:

Quote:
I'm trying to find a clean solution for making
an adapter which would pull the second (or first)
out of a pair<>. So far I only have this ugly thing
which only works for member functions (what my
friend needed.) Surely there is a more generic
adapter in, say, boost for this ? If so, I
haven't found it.

You can use boost::transform_iterator for that.
http://groups-beta.google.com/group/comp.lang.c++.moderated/msg/26b344960588d3ac

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Edmund McConnell
Guest





PostPosted: Tue May 10, 2005 12:02 pm    Post subject: Re: adapter for .second of pair<> ? Reply with quote

"Maxim Yegorushkin" <e-maxim (AT) yandex (DOT) ru> writes:

i guess you're looking for something like select1st<> and select2nd<>
which is an extension included in SGI STL.

http://www.sgi.com/tech/stl/select1st.html
http://www.sgi.com/tech/stl/select2nd.html

regards,
ed.

Quote:
On Sun, 08 May 2005 21:47:52 +0400, Kevin Bailey <noone (AT) nowhere (DOT) com> wrote:

I'm trying to find a clean solution for making
an adapter which would pull the second (or first)
out of a pair<>. So far I only have this ugly thing
which only works for member functions (what my
friend needed.) Surely there is a more generic
adapter in, say, boost for this ? If so, I
haven't found it.

You can use boost::transform_iterator for that.
http://groups-beta.google.com/group/comp.lang.c++.moderated/msg/26b344960588d3ac

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Kevin Bailey
Guest





PostPosted: Tue May 10, 2005 7:37 pm    Post subject: Re: adapter for .second of pair<> ? Reply with quote

Maxim Yegorushkin wrote:
Quote:

You can use boost::transform_iterator for that.

http://groups-beta.google.com/group/comp.lang.c++.moderated/msg/

26b344960588d3ac
Quote:


Thanks, it never occured to me to modify the iterators. In case anyone
really needs a functor approach, here is what I last gave my friend:

template <typename Operation>
struct apply_second_t
// :public std::unary_function<
// std::pair // typename Operation::result_type>
{
const Operation& _pf;

apply_second_t(
const Operation& pf)
:_pf(pf)
{
}

template <typename P1>
typename Operation::result_type operator()(
std::pair<P1, typename Operation::argument_type>& t)
{
return _pf(t.second);
}

template <typename P1>
typename Operation::result_type operator()(
const std::pair<P1, typename Operation::argument_type>&
t)
{
return _pf(t.second);
}
};

template <typename Operation>
apply_second_t<Operation> apply_second(
Operation pf)
{
return apply_second_t<Operation>(pf);
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
sga
Guest





PostPosted: Wed May 11, 2005 7:44 am    Post subject: Re: adapter for .second of pair<> ? Reply with quote

I use these simple functors quite a bit :

....
struct PairSecond
{
template< typename Pair >
typename Pair::second_type operator()( const Pair& p ) const
{
return ( p.second );
}
}

struct PairFirst
{
template< typename Pair >
typename Pair::first_type operator()( const Pair& p ) const
{
return ( p.first );
}
}

....
typedef std::pair< foo, bar > FooBarPair;
std::list< FooBarPair > pairList;
std::list< bar > barList;

std::transform( pairList.begin(),
pairList.end(),
std::back_inserter<etc>( pairList ),
PairSecond() );


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group