所以我有一个简单的例子:
#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))
答案 0 :(得分:7)
因为那是remove_if
的作用;第三个参数是一个谓词,用于测试是否删除元素。据推测,您的代码无法编译,因为shared_ptr
无法像函数一样被调用。
如果要删除具有特定值的元素,请使用remove
。