 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kevin Bailey Guest
|
Posted: Sun May 08, 2005 5:47 pm Post subject: adapter for .second of pair<> ? |
|
|
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
|
Posted: Mon May 09, 2005 6:17 am Post subject: Re: adapter for .second of pair<> ? |
|
|
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
|
Posted: Mon May 09, 2005 12:15 pm Post subject: Re: adapter for .second of pair<> ? |
|
|
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
|
|
| Back to top |
|
 |
Kevin Bailey Guest
|
Posted: Tue May 10, 2005 7:37 pm Post subject: Re: adapter for .second of pair<> ? |
|
|
Maxim Yegorushkin wrote:
26b344960588d3ac
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
|
Posted: Wed May 11, 2005 7:44 am Post subject: Re: adapter for .second of pair<> ? |
|
|
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 |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|