Search code examples
c++boost-lambdaboost-phoenix

Boost phoenix or lambda library problem: removing elements from a std::vector


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());

Solution

  • You need boost::labmda::bind to lambda-ify function calls, for example the length < 24 part becomes:

    bind(&string::length, _1) < 24
    

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

    int main()
    {
      vector<string> strings = getstrings();
      set<string> others = getothers();
      set<string>::const_iterator (set<string>::*findFn)(const std::string&) const = &set<string>::find;
      strings.erase(
        remove_if(strings.begin(), strings.end(),
            bind(&string::length, _1) < 24 &&
            bind(findFn, boost::ref(others), _1) == others.end()
          ), strings.end());
      copy(strings.begin(), strings.end(), ostream_iterator<string>(cout, ", "));
      return 0;
    }