使用 std::any_of 和 lambda 调用重载的相等运算符

Using std::any_of with lambda calling overloaded equality operator

我有以下 class:

Class Foo {
public:
    bool operator ==(const Foo& f);
    ...
private:
    set<pair<int,int>> points;
    ...
}

重载的相等运算符 returns 如果两个 Foo 对象具有相等的点集则为真。如果我这样使用它,它会按预期工作:

Foo a = Foo();
Foo b = Foo();
if (a == b) ...

我的问题是,为什么下面的编译失败?

vector<Foo> foos = ...
Foo c = ...
if (any_of(foos.begin(),foos.end(),[c](const Foo& f) { return (f == c); }))
{
    // stuff
} 

在你的 lambda 中,fconst。所以你不能在上面调用你的operator==,因为你的operator==不是const。所以解决这个问题:

bool operator==(const Foo& f) const;