自定义类的大小比较重载的两种方式

 方式一:

#include <iostream>
using namespace std;

struct Number
{
public:
    Number(int a, int b, int c)
     : first(a), second(b), third(c)
    {

    }

    Number(const Number & rhs)
     : first(rhs.first), second(rhs.second), third(rhs.third)
    {

    }

    bool operator < (const Number & rhs) const
    {
        if (first != rhs.first) {
            return (first < rhs.first);
        }
        if (second != rhs.second) {
            return (second < rhs.second);
        }
        if (third != rhs.third) {
            return (third < rhs.third);
        }
        return false;
    }

    bool operator > (const Number & rhs) const
    {
        return (rhs < *this);
    }

    bool operator <= (const Number & rhs) const
    {
        return (!(*this > rhs));
    }

    bool operator >= (const Number & rhs) const
    {
        return (!(*this < rhs));
    }

    bool operator != (const Number & rhs) const
    {
        return (*this < rhs || *this > rhs); // 或者写原始比较的代码
    }

    bool operator == (const Number & rhs) const
    {
        return (!(*this != rhs));
    }

    friend ostream & operator << (ostream & os, const Number & num);

private:
    const int first;
    const int second;
    const int third;
};

ostream & operator << (ostream & os, const Number & num)
{
    return (os << "{" << num.first << ", "
               << num.second << ", " << num.third << "}");
}

int main()
{
    Number lhs(1, 2, 5);
    Number rhs(1, 3, 4);

    cout << "lhs : " << lhs << endl;
    cout << "rhs : " << rhs << endl;
    cout << " < ? " << (lhs < rhs) << endl;
    cout << " > ? " << (lhs > rhs) << endl;
    cout << " <= ? " << (lhs <= rhs) << endl;
    cout << " >= ? " << (lhs >= rhs) << endl;
    cout << " == ? " << (lhs == rhs) << endl;
    cout << " != ? " << (lhs != rhs) << endl;

    return 0;
}

方式二:

#include <iostream>
using namespace std;

struct Number
{
public:
    Number(int a, int b, int c)
     : first(a), second(b), third(c)
    {

    }

    Number(const Number & rhs)
     : first(rhs.first), second(rhs.second), third(rhs.third)
    {

    }

    bool operator < (const Number & rhs) const
    {
        return (compare(rhs) < 0);
    }

    bool operator > (const Number & rhs) const
    {
        return (compare(rhs) > 0);
    }

    bool operator <= (const Number & rhs) const
    {
        return (compare(rhs) <= 0);
    }

    bool operator >= (const Number & rhs) const
    {
        return (compare(rhs) >= 0);
    }

    bool operator != (const Number & rhs) const
    {
        return (compare(rhs) != 0);
    }

    bool operator == (const Number & rhs) const
    {
        return (compare(rhs) == 0);
    }

    friend ostream & operator << (ostream & os, const Number & num);

private:
    int compare(const Number & rhs) const
    {
        if (first < rhs.first) {
            return -1;
        }
        else if (first > rhs.first) {
            return 1;
        }

        if (second < rhs.second) {
            return -1;
        }
        else if (second > rhs.second) {
            return 1;
        }

        if (third < rhs.third) {
            return -1;
        }
        else if (third > rhs.third) {
            return 1;
        }
        else {
            return 0;
        }
    }

private:
    const int first;
    const int second;
    const int third;
};

ostream & operator << (ostream & os, const Number & num)
{
    return (os << "{" << num.first << ", "
               << num.second << ", " << num.third << "}");
}

int main()
{
    Number lhs(1, 2, 5);
    Number rhs(1, 3, 4);

    cout << "lhs : " << lhs << endl;
    cout << "rhs : " << rhs << endl;
    cout << " < ? " << (lhs < rhs) << endl;
    cout << " > ? " << (lhs > rhs) << endl;
    cout << " <= ? " << (lhs <= rhs) << endl;
    cout << " >= ? " << (lhs >= rhs) << endl;
    cout << " == ? " << (lhs == rhs) << endl;
    cout << " != ? " << (lhs != rhs) << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值