简体   繁体   中英

Easiest way to compare data members, from two objects of the same type

I'd like to compare whether two objects (of the same type) have the same data member values. Does the STL have any way of doing this?

No, the standard library will generally use operator== to compare two elements in a range. You can specify a custom predicate or overload operator== , but there is no built-in operator== for two class types.

#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);
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM