4

I am trying to remove short strings from a vector.

std::vector<std::string> vec;

// ...

vec.erase(std::remove_if(vec.begin(),
                         vec.end(),
                         boost::bind(std::less<size_t>(),
                                     boost::bind(&std::string::length, _1),
                                     5),
          vec.end());

The compiler spits out a very large error message:

qwer.cpp:20: error: no matching function for call to 'remove_if(__gnu_cxx::__nor
mal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char
> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator
<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::al
locator<char> > > > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std:
:char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char,
 std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_strin
g<char, std::char_traits<char>, std::allocator<char> > > > >, boost::_bi::bind_t
<boost::_bi::unspecified, std::less<unsigned int>, boost::_bi::list2<boost::_bi:
:bind_t<unsigned int, boost::_mfi::cmf0<unsigned int, std::basic_string<char, st
d::char_traits<char>, std::allocator<char> > >, boost::_bi::list1<boost::arg<1>
> >, boost::_bi::value<int> > >, __gnu_cxx::__normal_iterator<std::basic_string<
char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_st
ring<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::b
asic_string<char, std::char_traits<char>, std::allocator<char> > > > >)'

The following solution works:

vec.erase(std::remove_if(vec.begin(),
                         vec.end(),
                         boost::bind(&std::string::length, _1) < 5),
          vec.end());

But I am still curious as to what I did wrong in the first version. Thanks!

1 Answer 1

5

It looks like you got your parenthesis off (there should be two after 5, one to close the bind, one to close the remove_if.) I am surprised this didn't give another error message about invalid token or something though, as the parens are clearly unbalanced (did you remove an extra close paren from the end while preparing for SO?). It looks like this is the case, because if you read the template arguments to remove_if in the error message, the next to last one is a boost bind_t, followed by another gnu::iterator.

6
  • Um, so it compiles now? Still looks off to me.. and I don't even know why your second "solution" even compiles. Jan 16, 2010 at 3:34
  • 3
    Bind overloads some operators to make writing predicates faster. It is a very neat feature: boost.org/doc/libs/1_41_0/libs/bind/bind.html#operators Jan 16, 2010 at 3:52
  • whoa. std::bind does not do that. Jan 16, 2010 at 3:55
  • boost still rocks C++0x on a few counts… things will get confusing. Jan 16, 2010 at 4:08
  • looks like tr1 bind doesn't even support nested bind statements correctly (or at least the VC implementation of tr1 bind). I thought they were a lot closer than they actually are.. Jan 16, 2010 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.