c++ - Boost phoenix or lambda library problem: removing elements from a std::vector -
i ran problem thought boost::lambda or boost::phoenix solve, not able syntax right , did way. wanted remove elements in "strings" less length , not in container.
this 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 ended doing 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 lambda-ify function calls, example length < 24 part becomes:
bind(&string::length, _1) < 24
edit
see "head geek"'s post why set::find tricky. got resolve correct set::find overload (so copied part), missed essential boost::ref() -- why comparison end() failed (the container 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; }
Comments
Post a Comment