我想比较两个(相同类型)的对象是否具有相同的数据成员值。STL有什么办法吗?
不可以,标准库通常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);
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句