通过 boost::bind 从向量中删除字符串

发布于 2024-08-18 05:37:22 字数 2163 浏览 2 评论 0原文

我正在尝试从向量中删除短字符串。

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

编译器吐出一条非常大的错误消息:

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

以下解决方案有效:

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

但我仍然很好奇我在第一个版本中做错了什么。谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-08-25 05:37:22

看起来你把括号关掉了(5后面应该有两个,一个用来关闭绑定,一个用来关闭remove_if。)我很惊讶这并没有给出关于无效令牌或其他东西的另一条错误消息,因为括号显然不平衡(在准备 SO 时,您是否从末尾删除了一个额外的近括号?)。看起来情况确实如此,因为如果您在错误消息中读取remove_if 的模板参数,那么倒数第二个是boost bind_t,后面是另一个gnu::iterator。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文