gpt4 book ai didi

c++ - 在模板化 vector 中查找重复项

转载 作者:行者123 更新时间:2023-11-28 06:07:04 24 4
gpt4 key购买 nike

<分区>

我有一个模板化的 class Timer

template<typename Clock, typename Duration>
class Timer
{
public:
void Register(const std::function<void(const std::chrono::time_point<Clock> &timePoint)> &eventHandler);

private:
std::vector<std::function<void(const std::chrono::time_point<Clock> &timePoint)>> eventHandlers;

// Some more stuff.
};

现在我想在 Register 中找到重复的条目,这就是问题所在。
我尝试了使用 std::find 和简单的 for 循环的不同方法。

到目前为止我能得到的最好的(为了可读性删除了命名空间):

typename vector<function<void(const time_point<Clock> &timePoint)>>::iterator it;
for(it = eventHandlers.begin(); it != eventHandlers.end(); ++it)
{
}

我的问题是比较Registerit的参数。
我尝试了几件事,例如:

typename function<void(const time_point<Clock> &timePoint)> &f = &(*it);
if(f == eventHandler)
{
// Duplicate
}

...和其他一些更复杂的方法,但都没有用。
我在解决这个问题时遇到了一些麻烦,因为我通常使用 C# 编写代码,因此我不习惯使用 C++ 进行模板化。
我该如何解决这个问题,是否有更优雅的解决方案来解决这个问题?

编辑:
我通常会收到此错误:

binary '==': no operator found which takes a left-hand operand of type 'std::function<void (const std::chrono::time_point<std::chrono::system_clock,std::chrono::system_clock::duration> &)>' (or there is no acceptable conversion)

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