我正在使用用户自定义类型集和自定义比较函数。当我尝试在集合之间使用==
运算符时,出现编译时错误。我错过了什么?与自定义比较功能设置相等
#include <cassert>
#include <set>
// my user-defined type
struct IntWrapper {
int value;
};
// my compare function
struct LessComparer {
bool operator()(const IntWrapper& lhs, const IntWrapper& rhs) const {
return lhs.value < rhs.value;
}
};
int main() {
std::set<IntWrapper, LessComparer> s;
assert(s == s); // I would expect this to work
}
你的'operator =='(和'operator!=')在哪里? – Toris
自定义比较器用于比较集合中的元素,不用于比较集合 – Loreto
这很有趣,但[this](http://ideone.com/tn1huh)起作用。不知道为什么,可能是编译器认为's'是一个函数。 – ilotXXI