所以我有下面的简单示例:

#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>

using namespace std;

struct foo
{};


int main(void)
{
  vector<shared_ptr<foo>> v;

  auto f1 = make_shared<foo>();
  auto f2 = make_shared<foo>();
  auto f3 = make_shared<foo>();
  auto f4 = make_shared<foo>();

  v.push_back(f1);
  v.push_back(f2);
  v.push_back(f3);
  v.push_back(f4);

  cout << v.size() << endl;

  v.erase(remove_if(begin(v), end(v), f2), end(v));

  cout << v.size() << endl;
}

为什么此remove_if认为f2是谓词而不是我要查找的值?我真的需要在这里提供谓词才能使它起作用吗?还是我在这里做错了什么?

(注意:编译器:gcc 4.8.2 -std = c++ 11)

编辑:应该有rtfm!无论如何-这是编译器的输出:
In file included from /usr/local/gcc/4.8.2/include/c++/4.8/algorithm:62:0,
                 from remove_if.cpp:4:
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h: In instantiation of '_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx::__normal_iterator<std::shared_ptr<foo>*, std::vector<std::shared_ptr<foo> > >; _Predicate = std::shared_ptr<foo>]':
remove_if.cpp:29:41:   required from here
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:1150:33: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
         if(!bool(__pred(*__first)))
                                 ^
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h: In instantiation of '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::shared_ptr<foo>*, std::vector<std::shared_ptr<foo> > >; _Predicate = std::shared_ptr<foo>]':
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:4465:41:   required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<std::shared_ptr<foo>*, std::vector<std::shared_ptr<foo> > >; _Predicate = std::shared_ptr<foo>]'
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:1144:64:   required from '_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx::__normal_iterator<std::shared_ptr<foo>*, std::vector<std::shared_ptr<foo> > >; _Predicate = std::shared_ptr<foo>]'
remove_if.cpp:29:41:   required from here
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:214:23: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
    if (__pred(*__first))
                       ^
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:218:23: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
    if (__pred(*__first))
                       ^
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:222:23: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
    if (__pred(*__first))
                       ^
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:226:23: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
    if (__pred(*__first))
                       ^
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:234:23: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
    if (__pred(*__first))
                       ^
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:238:23: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
    if (__pred(*__first))
                       ^
/usr/local/gcc/4.8.2/include/c++/4.8/bits/stl_algo.h:242:23: error: no match for call to '(std::shared_ptr<foo>) (std::shared_ptr<foo>&)'
    if (__pred(*__first))

最佳答案

因为那是remove_if所做的;第三个参数是谓词,用于测试是否删除元素。据推测,由于shared_ptr不能像函数一样被调用,因此您的代码无法编译。

如果要删除具有特定值的元素,请使用 remove

关于c++ - 为什么std::remove_if认为shared_ptr <T>是谓词?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26505553/

10-13 08:23