我想比较两个对象(相同类型)是否具有相同的数据成员值。 STL有没有办法做到这一点?
答案 0 :(得分:1)
不,标准库通常会使用operator==
来比较范围中的两个元素。您可以指定自定义谓词或重载operator==
,但两种类类型没有内置operator==
。
#include <tuple>
struct Foo
{
int a, b, c;
bool operator==(const Foo& other)
{
return std::tie(a, b, c) == std::tie(other.a, other.b, other.c);
}
};