gpt4 book ai didi

c++ - 为什么 std::remove_if 认为 shared_ptr 是谓词?

转载 作者:搜寻专家 更新时间:2023-10-30 23:56:57 25 4
gpt4 key购买 nike

所以我有下面这个简单的例子:

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com