gpt4 book ai didi

c++ - 在 find_if 不接受的泛型上运行的仿函数

转载 作者:行者123 更新时间:2023-11-28 01:39:16 28 4
gpt4 key购买 nike

让我们考虑以下代码部分:

template <class T, class Comp>
Class MyClass {

public:
struct my_functor {
public:
my_functor(const T w) : wanted(w) {}
bool operator()(const T t) {
return customEquals(t,wanted);
}
private:
T wanted;
};


private:
std::vector<T> container;
bool customEquals(T a, T b) { ... }
};

当我进一步尝试调用

std::find_if(container.begin(),container.end(),v,my_functor(v));

其中 v 是 T const& 类型的输入,我得到以下编译错误(g++,C++14):

error: no matching function for call to 'find_if(std::vector<int>::iterator,std::vector<int>::iterator,const int&,MyClass<int,bool(*)(int,int)>::my_functor)

T 在此特定示例中为 int

这里似乎有什么问题?

PS:我已经包含了所有必需的 header (算法、 vector 、函数式...),所以问题不存在。

最佳答案

What seems to be the problem here?

不太确定您期望此代码起作用的原因。

首先,您需要检查 std::find_if过载。没有接受值查找和自定义谓词的重载,只接受谓词。

然后,如果您使用 my_functor,代码应该如下所示:

std::find_if(std::cbegin(container), std::cend(container),
MyClass<T, Comp>::my_functor(v));

其中 TComp 是一些类型。

关于c++ - 在 find_if 不接受的泛型上运行的仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008477/

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