gpt4 book ai didi

c++ - 列表的 Remove_if 谓词

转载 作者:搜寻专家 更新时间:2023-10-31 00:47:52 26 4
gpt4 key购买 nike

为什么当我使用模板函数谓词时这不起作用。

class A
{
int x;
public:
A(int X):x(X){}
int getX() const {return x;}
};

template <typename T>
bool less_than_two(T const &a)
{
return (a.getX()<2);
}

int main()
{
list <A> l;

for (int i = 0; i < 5; i++)
l.push_back(i);

l.remove_if(less_than_two);
}

最佳答案

您需要实例化类型为 A 的模板.所以你需要做 l.remove_if(less_than_two<A>);

关于c++ - 列表的 Remove_if 谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3351048/

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