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 

Replace a loop with an STL algorithm?

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





PostPosted: Thu Dec 23, 2004 9:13 pm    Post subject: Replace a loop with an STL algorithm? Reply with quote



Item 43 of Effective STL suggests that I prefer alogorithm calls to
hand-written loops. The suggestion has been very helpful in general, but
this loop has me stumped.

The code below works fine with the hand-written loop.

void Board::FindCellsToDel(void)
{
set<int> toBeDel;

//code to populate set omitted

for (set<int>::const_iterator i=toBeDel.begin(); i!=toBeDel.end(); ++i)
DelCell(*i);
}

void Board::DelCell(int itemID)
{
//code to delete items omitted
}

I want to use the for_each algorithm. If DelCell were a free function rather
than a member function of Board I could write:

for_each(toBeDel.begin(), toBeDel.end(), DelCell);

but DelCell needs access to much private data in class Board.

I don't understand how to call the member function and pass the deferenced
iterator. I have been experimenting with combinations of mem_fun_ref and
bind2nd, but nothing seems to compile.

Can anyone straighten me out?

Thanks,

David


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
dave_abrahams
Guest





PostPosted: Fri Dec 24, 2004 9:49 am    Post subject: Re: Replace a loop with an STL algorithm? Reply with quote



#include <boost/bind.hpp>

.....
for_each(
toBeDel.begin(), toBeDel.end()
, boost::bind(&Board::DelCEll, this, _1));


http://www.boost.org/libs/bind/bind.html for more info.
HTH,
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com


[ 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





PostPosted: Fri Dec 24, 2004 9:51 am    Post subject: Re: Replace a loop with an STL algorithm? Reply with quote



David Levy <dml.pubic.nospam (AT) sbcglobal (DOT) net> wrote:

Quote:
Item 43 of Effective STL suggests that I prefer alogorithm calls to
hand-written loops. The suggestion has been very helpful in general, but
this loop has me stumped.

The code below works fine with the hand-written loop.

void Board::FindCellsToDel(void)
{
set<int> toBeDel;

//code to populate set omitted

for (set<int>::const_iterator i=toBeDel.begin(); i!=toBeDel.end(); ++i)
DelCell(*i);
}

void Board::DelCell(int itemID)
{
//code to delete items omitted
}

I want to use the for_each algorithm. If DelCell were a free function rather
than a member function of Board I could write:

for_each(toBeDel.begin(), toBeDel.end(), DelCell);

the short answer is:

std::for_each(toBeDel.begin(),toBeDel.end(),
std::bind1st(std::mem_fun(&Board::DelCel),this));

first create an functor that takes a Board * and and int as arguments
say F1, and then create a second functor that takes a int as its
qrgument, calling a instance of F1 with the first parameter fixed as the
this pointer. [Looks worse than it really is] what you end up with is
a functor that is called with an int argument executing
(this->*DelCel)(x), for an int x.

if this is 'too messy'

class MyFunc
{
Board *p_board;
public:
explicit MyFunc(Board *a):p_board(a){}
void operator () (int x)
{
(p_boara->*DelCel)(x);
}
};

is what you are creating with the std functions mem_fun,bind1st above.

std::for_each(toBeDel.begin(),toBeDel.end(),MyFunc(this)); is what the
for_each reduces to.

Hope this helps some


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