On 17 jan, 17:59, "Daniel T." <danie...@earthlink.net> wrote:
On Jan 17, 10:49 am, nguillot <nicolas.guil...@gmail.com> wrote:
Hello
I used to loop on a std::map<k, d> to act on the data (d) like that (d=
being a class with setIntMember method):
typedef std::map<k, d> tMap;
struct setIntMember
{
setIntMember(int j) : i(j) {}
void operator()(tMap::value_type& pair)
{ pair.second.setIntMember(i); }
int i;
};
The loop being:
for_each(m.begin(), m.end(), setIntMember(4));
I searched for a method to write all in the for_each loop (to avoid
increasing the functors).
We need a function returning the second member of the pair
tMap::value_type,
and their is the SGI template:
template<typename _Pair>
struct Select2nd : public unary_function<_Pair,
typename _Pair::se=
_1)));
but the d::Trace method is called on a temporary object, so I cannot
modify the content of the objects contained in the map.
Some ideas?
Just use the functor you created. It's simple and clean... You might
want to generalized it a little bit:
template < typename Pair, typename Op >
class CallFuncOn2nd_t {
Op fn;
public:
CallFuncOn2nd_t( Op fn ): fn(fn) { }
typename Op::result_type operator()( Pair& v ) const {
return fn( v.second );
}
};
template < typename Pair, typename Op >
CallFuncOn2nd_t<Pair, Op> callFuncOn2nd( Op fn ) {
return CallFuncOn2nd_t<Pair, Op>( fn );
}
for_each(m.begin(), m.end(),
callFuncOn2nd<tMap::value_type>(bind2nd(mem_fun_ref(&d::setIntMember),
4)));
Ok, thanks, that's was exactly what I wanted to do!
But callFuncOn2nd<tMap::value_type>(bind2nd... fails to compile: the
second template argument seems to be required.
We must set the second template argument as a class that
has ::result_type, so I tried a std::unary_function<d, bool> (if
d::setIntMember returns a bool), but even if bind2nd could be
converted to unary_function (what fails) a unary function doesn't have
a () operator.
And I didn't manage write the second argument to be
std::binder2nd<std::binary_function<d*, int, bool> > or something like
that: it doesn't compile.
So the question: how write the for_each line?
Hmm, the for_each line I wrote above compiles fine for me. What