如何使用一些成员简化 class 的 operator< 以便在 std 容器中使用

How to simplify the operator< for a class with some members for the use in std containers

我有一个带有 4 个 ID 的简单 class。

我想将此 class 用作 std::mapstd::set 或任何其他标准容器中的键。所以我只是实现了示例代码中看到的 operator<

序列应该像 SQL ORDER BY m_lIdTicket, m_lIdUser, m_lIdAddress, m_lIdRole

struct S_DATA_CRMTICKETROLES 
{
public:
...
    bool operator<(const S_DATA_CRMTICKETROLES &r) const
    {
        if (m_lIdTicket<r.m_lIdTicket)
            return true;
        else if (m_lIdTicket>r.m_lIdTicket)
            return false;

        if (m_lIdUser<r.m_lIdUser)
            return true;
        else if (m_lIdUser>r.m_lIdUser)
            return false;

        if (m_lIdAddress<r.m_lIdAddress)
            return true;
        else if (m_lIdAddress>r.m_lIdAddress)
            return false;

        if (m_lIdRole<r.m_lIdRole)
            return true;
        else if (m_lIdRole>r.m_lIdRole)
            return false;

        // Seams to be equal. Code placed here if other members are added and to 
        // keep the code in a similar way for each member added.
        return false;
    }
    bool operator==(const S_DATA_CRMTICKETROLES &r) const
    {
        return !operator<(r) && !r.operator<(*this);
    }
    bool operator!=(const S_DATA_CRMTICKETROLES &r) const
    {
        return !operator==(r);
    }
... 
    long m_lIdTicket, m_lIdUser, m_lIdAddress, m_lIdRole;
};

另外,为了方便和在其他 classes 中使用,我实现了 operator==operator!= .

我的问题:有没有更有效的方法来编写这样的代码?

std::tie 在 C++20 的 operator <=>bool operator==(const S_DATA_CRMTICKETROLES&) = default.

之前有帮助
struct S_DATA_CRMTICKETROLES 
{
public:
    auto as_tuple() const {
        return std::tie(m_lIdTicket, m_lIdUser, m_lIdAddress, m_lIdRole);
    }


    bool operator<(const S_DATA_CRMTICKETROLES &r) const
    {
        return as_tuple() < r.as_tuple();
    }
    bool operator==(const S_DATA_CRMTICKETROLES &r) const
    {
        return as_tuple() == r.as_tuple();
    }
    bool operator!=(const S_DATA_CRMTICKETROLES &r) const
    {
        return as_tuple() != r.as_tuple();
    }
 
    long m_lIdTicket, m_lIdUser, m_lIdAddress, m_lIdRole;
};