472,978 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 software developers and data experts.

for_each loop on a map without a functor

Hello

I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):

typedef std::map<k, dtMap;

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::second_type>
{
typename _Pair::second_type& operator()(_Pair& __x) const
{ return __x.second; }

const typename _Pair::second_type& operator()(const _Pair&
__x) const
{ return __x.second; }
};

But I didn't manage to write a single line to perform the for_each
loop, thanks to Select2nd with for_each, bind2nd and mem_fun and
mem_fun1

I didn't manage with boost::bind (on VC++ 6.0):
With boost, I can access the objects like that:
for_each(m.begin(), m.end(),
boost::bind(&d::Trace, boost::bind(&tMap::value_type::second,
_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? Thanks in advance.

Nicolas.
Jan 17 '08 #1
9 3952
On Jan 17, 10:49*am, nguillot <nicolas.guil...@gmail.comwrote:
Hello

I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):

* * typedef std::map<k, dtMap;

* * 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::second_type>
* * {
* * * * typename _Pair::second_type& operator()(_Pair& __x) const
* * * * { return __x.second; }

* * * * const typename _Pair::second_type& operator()(const _Pair&
__x) const
* * * * { return __x.second; }
* * };

But I didn't manage to write a single line to perform the for_each
loop, thanks to Select2nd with for_each, bind2nd and mem_fun and
mem_fun1

I didn't manage with boost::bind (on VC++ 6.0):
With boost, I can access the objects like that:
* * for_each(m.begin(), m.end(),
* * * * boost::bind(&d::Trace, boost::bind(&tMap::value_type::second,
_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, OpcallFuncOn2nd( Op fn ) {
return CallFuncOn2nd_t<Pair, Op>( fn );
}

for_each(m.begin(), m.end(),

callFuncOn2nd<tMap::value_type>(bind2nd(mem_fun_re f(&d::setIntMember),
4)));

Jan 17 '08 #2
On Jan 17, 10:49 am, nguillot <nicolas.guil...@gmail.comwrote:
Hello

I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):

typedef std::map<k, dtMap;

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).
[snip]
>
I didn't manage with boost::bind (on VC++ 6.0):
With boost, I can access the objects like that:
for_each(m.begin(), m.end(),
boost::bind(&d::Trace, boost::bind(&tMap::value_type::second,
_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.
For the boost::bind part, your case looks very much like what is
discussed here: http://lists.boost.org/boost-users/2007/01/24599.php
(Look at Peter Dimov's reply)
Jan 17 '08 #3
On 17 jan, 17:59, "Daniel T." <danie...@earthlink.netwrote:
On Jan 17, 10:49 am, nguillot <nicolas.guil...@gmail.comwrote:
Hello
I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):
typedef std::map<k, dtMap;
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::second_type>
{
typename _Pair::second_type& operator()(_Pair& __x) const
{ return __x.second; }
const typename _Pair::second_type& operator()(const _Pair&
__x) const
{ return __x.second; }
};
But I didn't manage to write a single line to perform the for_each
loop, thanks to Select2nd with for_each, bind2nd and mem_fun and
mem_fun1
I didn't manage with boost::bind (on VC++ 6.0):
With boost, I can access the objects like that:
for_each(m.begin(), m.end(),
boost::bind(&d::Trace, boost::bind(&tMap::value_type::second,
_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, OpcallFuncOn2nd( Op fn ) {
return CallFuncOn2nd_t<Pair, Op>( fn );

}

for_each(m.begin(), m.end(),

callFuncOn2nd<tMap::value_type>(bind2nd(mem_fun_re f(&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?
Or how to modify the callFuncOn2nd to auto discover the type (that
woulkd be nice, but as Op is the typeof fn (member of the class), we
have to put Op a a class template argument...

Thanks for help.

Nicolas.
Jan 18 '08 #4
On Jan 18, 5:45*am, nguillot <nicolas.guil...@gmail.comwrote:
On 17 jan, 17:59, "Daniel T." <danie...@earthlink.netwrote:


On Jan 17, 10:49 am, nguillot <nicolas.guil...@gmail.comwrote:
Hello
I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):
* * typedef std::map<k, dtMap;
* * 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::second_type>
* * {
* * * * typename _Pair::second_type& operator()(_Pair& __x) const
* * * * { return __x.second; }
* * * * const typename _Pair::second_type& operator()(const _Pair&
__x) const
* * * * { return __x.second; }
* * };
But I didn't manage to write a single line to perform the for_each
loop, thanks to Select2nd with for_each, bind2nd and mem_fun and
mem_fun1
I didn't manage with boost::bind (on VC++ 6.0):
With boost, I can access the objects like that:
* * for_each(m.begin(), m.end(),
* * * * boost::bind(&d::Trace, boost::bind(&tMap::value_type::second,
_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, OpcallFuncOn2nd( Op fn ) {
* * return CallFuncOn2nd_t<Pair, Op>( fn );
}
for_each(m.begin(), m.end(),
callFuncOn2nd<tMap::value_type>(bind2nd(mem_fun_re f(&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
compiler are you using?
Jan 18 '08 #5
On 18 jan, 21:03, "Daniel T." <danie...@earthlink.netwrote:
On Jan 18, 5:45 am, nguillot <nicolas.guil...@gmail.comwrote:
On 17 jan, 17:59, "Daniel T." <danie...@earthlink.netwrote:
On Jan 17, 10:49 am, nguillot <nicolas.guil...@gmail.comwrote:
Hello
I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):
typedef std::map<k, dtMap;
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::second_type>
{
typename _Pair::second_type& operator()(_Pair& __x) const
{ return __x.second; }
const typename _Pair::second_type& operator()(const _Pair&
__x) const
{ return __x.second; }
};
But I didn't manage to write a single line to perform the for_each
loop, thanks to Select2nd with for_each, bind2nd and mem_fun and
mem_fun1
I didn't manage with boost::bind (on VC++ 6.0):
With boost, I can access the objects like that:
for_each(m.begin(), m.end(),
boost::bind(&d::Trace, boost::bind(&tMap::value_type::second,
_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, OpcallFuncOn2nd( Op fn ) {
return CallFuncOn2nd_t<Pair, Op>( fn );
}
for_each(m.begin(), m.end(),
callFuncOn2nd<tMap::value_type>(bind2nd(mem_fun_re f(&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
compiler are you using?
I tried on VC++ 6.0 and on visual studio 2005...
and you ?
Jan 18 '08 #6
On Jan 18, 3:41 pm, nguillot <nicolas.guil...@gmail.comwrote:
On 18 jan, 21:03, "Daniel T." <danie...@earthlink.netwrote:
On Jan 18, 5:45 am, nguillot <nicolas.guil...@gmail.comwrote:
So the question: how write the for_each line?
Hmm, the for_each line I wrote above compiles fine for me. What
compiler are you using?

I tried on VC++ 6.0 and on visual studio 2005...
and you
Visual Sutdio 2005 compiles the following just fine:
=== begine code ===
#include <algorithm>
#include <map>

using namespace std;

typedef int k;

class d {
public:
void setIntMember( int i ) { }
};

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, OpcallFuncOn2nd( Op fn ) {
return CallFuncOn2nd_t<Pair, Op>( fn );
}

typedef std::map<k, dtMap;

int main(){
tMap m;
for_each(m.begin(), m.end(),

callFuncOn2nd<tMap::value_type>( bind2nd( mem_fun_ref( &d::setIntMember ),
4 ) ) );
}
=== end code ===
Jan 18 '08 #7
On Jan 18, 12:49 am, nguillot <nicolas.guil...@gmail.comwrote:
Some ideas? Thanks in advance.

Nicolas.
If you are brave enough to use my radical library:
http://p-stade.sourceforge.net/oven/index.html

PSTADE_OVEN_FOREACH (v, m|oven::map_values) {
// use and modify v...
}
Regards,

--
Shunsuke Sogame
Jan 19 '08 #8
nguillot <nicolas.guil...@gmail.comwrote:
I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):

typedef std::map<k, dtMap;

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

Some ideas? Thanks in advance.
I finally got it...

class Foo {
public:
void bar( int i ) { }
};

typedef map<int, Foomap_t;

int main() {
map_t pot;
for_each( pot.begin(), pot.end(),
bind( &Foo::bar, bind(&map_t::value_type::second, _1 ), 4 ) );
}

I verified that this does in fact call Foo::bar on the object within
the map, not on a temporary.

Jan 19 '08 #9
In article <4833611d-1e00-4984-bf4e-313b2f7fb957@
21g2000hsj.googlegroups.com>, ni*************@gmail.com says...
Hello

I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):

typedef std::map<k, dtMap;

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).
In this case, I'm pretty sure a simple for loop is really much more
readable than anything using for_each:

for (std::map<k,d>::iterator p=m.begin(), p!=m.end(), ++p)
p->second = 4;

Boost::lambda is often useful when you want to avoid (explicitly)
creating a functor, but I don't believe it works for this particular
case. As we've already seen in the rest of the thread, the code to make
for_each do the job ends up ugly and MUCH less readable than an explicit
loop. I know we'd all like to use an algorithm, but in this case the
cost is high and the benefit essentially nonexistent.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 19 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean | last post by:
#include <iostream> #include <algorithm> struct functor { ~functor() {std::cout << 'D';} void operator()(int a) {std::cout << a;} }; int main() { int a = {1,2,3,4,5}; functor f;
5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
18
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what...
1
by: trash | last post by:
I do this kindda stuff often: class CVisitableObjectVector : public std::vector<Loki::BaseVisitable<>*> { void Enable(bool enable); .... };
11
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its...
6
by: Michal Wyrebski | last post by:
Hello, I'm new in this group and I don't know if my questions are too silly, but I'm intermediate programmer and don't have enought experience. Please be charitable. I don't know how to write...
9
by: shaun | last post by:
I am working on code where I am handed a vector of pointers vector<T*> or a map<std::string, T*>, and I have to delete the objects and set the pointers to zero. I have been using a 'for' loop and...
9
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the...
3
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.