I recently ran into a problem that I thought boost::lambda or boost::phoenix could help be solve, but I was not able to get the syntax right and so I did it another way. What I wanted to do was remove all the elements in ‘strings’ that were less than a certain length and not in another container.
This is my first try:
std::vector<std::string> strings = getstrings(); std::set<std::string> others = getothers(); strings.erase(std::remove_if(strings.begin(), strings.end(), (_1.length() < 24 && others.find(_1) == others.end())), strings.end());
How I ended up doing it was this:
struct Discard { bool operator()(std::set<std::string> &cont, const std::string &s) { return cont.find(s) == cont.end() && s.length() < 24; } }; lines.erase(std::remove_if( lines.begin(), lines.end(), boost::bind<bool>(Discard(), old_samples, _1)), lines.end());
You need boost::labmda::bind to lambda-ify function calls, for example the length < 24 part becomes:
EDIT
See ‘Head Geek”s post for why set::find is tricky. He got it to resolve the correct set::find overload (so I copied that part), but he missed an essential boost::ref() — which is why the comparison with end() always failed (the container was copied).