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 

Re: STL functional library

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





PostPosted: Mon Mar 07, 2005 11:18 pm    Post subject: Re: STL functional library Reply with quote



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





PostPosted: Tue Mar 08, 2005 11:02 am    Post subject: Re: STL functional library Reply with quote



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





PostPosted: Tue Mar 08, 2005 9:17 pm    Post subject: Re: STL functional library Reply with quote



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





PostPosted: Sat Mar 12, 2005 8:08 pm    Post subject: Re: STL functional library Reply with quote

"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





PostPosted: Sun Mar 13, 2005 11:19 am    Post subject: Re: STL functional library Reply with quote

"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





PostPosted: Sun Mar 13, 2005 12:28 pm    Post subject: Re: STL functional library Reply with quote

"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





PostPosted: Sat Mar 19, 2005 12:53 am    Post subject: Re: STL functional library Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.