 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Levy Guest
|
Posted: Thu Dec 23, 2004 9:13 pm Post subject: Replace a loop with an STL algorithm? |
|
|
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
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Fri Dec 24, 2004 9:51 am Post subject: Re: Replace a loop with an STL algorithm? |
|
|
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 |
|
 |
|
|
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
|
|