 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Roland Pibinger Guest
|
Posted: Mon Mar 07, 2005 11:18 pm Post subject: Re: STL functional library |
|
|
On 7 Mar 2005 06:19:13 -0500, Ari Lamstein
<lamstein (AT) gaia (DOT) cc.gatech.edu> wrote:
| Quote: | Hi,
I'm trying to learn the right syntax for mem_fun, bind1st, etc. in the STL
functional library. All of the resources that I've been able to find deal
with e.g. a container of ints, but the syntax is very different if you
have a container of pointers to user-defined data, and you want to call
a member function on each of those objects, and supply that function
with an argument.
|
[...]
| Quote: | int main (int argc, char * const argv[]) {
using namespace std;
vector<Widget*> vWidgets;
for (int i = 0; i < 10; i++)
{
vWidgets.push_back(new Widget());
}
for_each(vWidgets.begin(), vWidgets.end(),
mem_fun(&Widget::PrintNum));
for_each(vWidgets.begin(), vWidgets.end(),
mem_fun(&Widget::IncNum));
// what's the right syntax for the below line?
//for_each(vWidgets.begin(), vWidgets.end(),
bind_2nd(mem_fun(&Widget::IncNumBy)(5)));
|
These binders and adapters never really worked out. The for loop is
more generic and versatile than for_each. Try
for (vector
iter != vWidgets.end(); ++iter) {
(**iter).IncNumBy(5);
}
Best wishes,
Roland Pibinger
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Tue Mar 08, 2005 11:02 am Post subject: Re: STL functional library |
|
|
Ari Lamstein wrote:
| Quote: | // what's the right syntax for the below line?
for_each(vWidgets.begin(), vWidgets.end(),
bind_2nd(mem_fun(&Widget::IncNumBy)(5)));
|
for_each(vWidgets.begin(), vWidgets.end(),
bind2nd(mem_fun(&Widget::IncNumBy), 5));
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Tue Mar 08, 2005 9:17 pm Post subject: Re: STL functional library |
|
|
I have to say that since I switched to using Boost.Bind (see
http://www.boost.org) I never looked back.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Early Ehlinger Guest
|
Posted: Sat Mar 12, 2005 8:08 pm Post subject: Re: STL functional library |
|
|
"Nicola Musatti" <nicola.musatti (AT) gmail (DOT) com> wrote :
| Quote: | I have to say that since I switched to using Boost.Bind (see
http://www.boost.org) I never looked back.
|
I have had a similar experience. With regards to the OP's code...
for_each
( vWidgets.begin(), vWidgets.end()
, boost::bind( &Widget::PrintNum , _1 , 5 ) );
is IMHO much more readable.
I used to have a bunch of headers defining binding templates,
member-function-calling-functors, etc., in my library to attempt to augment
the Standard and make the algorithms more usable. When I found out about
Boost.Bind, I quickly added
#error "USE BOOST.BIND INSTEAD OF THIS!"
to all of those headers, forcing myself to switch my entire codebase over.
I don't think I could write a stronger testimonial for Boost.Bind.
--
--
Best Regards,
- Early Ehlinger - President&CEO - ResPower, Inc - www.ResPower.com
- 2.8+ THz Self-Service Render Farm from $0.25/GHz*Hr
[Yes, 2.8 TeraHertz - 2,800 Gigahertz. 2,800,000 Megahertz.
2,800,000,000 Kilohertz. 2,800,000,000,000 Clock Cycles Per Second.
Every second. 24 hours a day. 7 Days a week. Upload your 3D scenes and
feel the speed!]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Early Ehlinger Guest
|
Posted: Sun Mar 13, 2005 11:19 am Post subject: Re: STL functional library |
|
|
"Nicola Musatti" <nicola.musatti (AT) gmail (DOT) com> wrote :
| Quote: | I have to say that since I switched to using Boost.Bind (see
http://www.boost.org) I never looked back.
|
I have had a similar experience. With regards to the OP's code...
for_each
( vWidgets.begin(), vWidgets.end()
, boost::bind( &Widget::PrintNum , _1 , 5 ) );
is IMHO much more readable.
I used to have a bunch of headers defining binding templates,
member-function-calling-functors, etc., in my library to attempt to augment
the Standard and make the algorithms more usable. When I found out about
Boost.Bind, I quickly added
#error "USE BOOST.BIND INSTEAD OF THIS!"
to all of those headers, forcing myself to switch my entire codebase over.
I don't think I could write a stronger testimonial for Boost.Bind.
--
--
Best Regards,
- Early Ehlinger - President&CEO - ResPower, Inc - www.ResPower.com
- 2.8+ THz Self-Service Render Farm from $0.25/GHz*Hr
[Yes, 2.8 TeraHertz - 2,800 Gigahertz. 2,800,000 Megahertz.
2,800,000,000 Kilohertz. 2,800,000,000,000 Clock Cycles Per Second.
Every second. 24 hours a day. 7 Days a week. Upload your 3D scenes and
feel the speed!]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Early Ehlinger Guest
|
Posted: Sun Mar 13, 2005 12:28 pm Post subject: Re: STL functional library |
|
|
"Nicola Musatti" <nicola.musatti (AT) gmail (DOT) com> wrote :
| Quote: | I have to say that since I switched to using Boost.Bind (see
http://www.boost.org) I never looked back.
|
I have had a similar experience. With regards to the OP's code...
for_each
( vWidgets.begin(), vWidgets.end()
, boost::bind( &Widget::PrintNum , _1 , 5 ) );
is IMHO much more readable.
I used to have a bunch of headers defining binding templates,
member-function-calling-functors, etc., in my library to attempt to augment
the Standard and make the algorithms more usable. When I found out about
Boost.Bind, I quickly added
#error "USE BOOST.BIND INSTEAD OF THIS!"
to all of those headers, forcing myself to switch my entire codebase over.
I don't think I could write a stronger testimonial for Boost.Bind.
--
--
Best Regards,
- Early Ehlinger - President&CEO - ResPower, Inc - www.ResPower.com
- 2.8+ THz Self-Service Render Farm from $0.25/GHz*Hr
[Yes, 2.8 TeraHertz - 2,800 Gigahertz. 2,800,000 Megahertz.
2,800,000,000 Kilohertz. 2,800,000,000,000 Clock Cycles Per Second.
Every second. 24 hours a day. 7 Days a week. Upload your 3D scenes and
feel the speed!]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Early Ehlinger Guest
|
Posted: Sat Mar 19, 2005 12:53 am Post subject: Re: STL functional library |
|
|
"Nicola Musatti" <nicola.musatti (AT) gmail (DOT) com> wrote :
| Quote: | I have to say that since I switched to using Boost.Bind (see
http://www.boost.org) I never looked back.
|
I have had a similar experience. With regards to the OP's code...
for_each
( vWidgets.begin(), vWidgets.end()
, boost::bind( &Widget::PrintNum , _1 , 5 ) );
is IMHO much more readable.
I used to have a bunch of headers defining binding templates,
member-function-calling-functors, etc., in my library to attempt to augment
the Standard and make the algorithms more usable. When I found out about
Boost.Bind, I quickly added
#error "USE BOOST.BIND INSTEAD OF THIS!"
to all of those headers, forcing myself to switch my entire codebase over.
I don't think I could write a stronger testimonial for Boost.Bind.
--
--
Best Regards,
- Early Ehlinger - President&CEO - ResPower, Inc - www.ResPower.com
- 2.8+ THz Self-Service Render Farm from $0.25/GHz*Hr
[Yes, 2.8 TeraHertz - 2,800 Gigahertz. 2,800,000 Megahertz.
2,800,000,000 Kilohertz. 2,800,000,000,000 Clock Cycles Per Second.
Every second. 24 hours a day. 7 Days a week. Upload your 3D scenes and
feel the speed!]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|