简单的class及运算符重载

Posted yeran

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的class及运算符重载相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


class Action
{
    public:
    int room;
    int time;
    int type;

    Action(int room_, int time_, int type_)
    {
        room = room_;
        time = time_;
        type = type_;
    };
    show()
    {
    cout<<"the message is "<<room<<" "<<time<<" "<<type<<endl;
    return 0;
    };

    bool operator< (const Action &other) const {
        if(time<other.time) return true;
        else if(time==other.time && type<other.type) return true;
        else if(time==other.time && type==other.type && room<other.room) return true;
        return false;
        }

};

int main()
{
    Action t1(1,22,3);
    Action t2(1,4,3);

    vector<Action> action;

    action.push_back(t1);
    action.push_back(t2);

    sort(action.begin(), action.end());
//  for (int i =0 ; i< action.size(); i++)
//      action[i].show();
    return 0;
}

以上是关于简单的class及运算符重载的主要内容,如果未能解决你的问题,请参考以下文章

C++ 运算符重载

String简单实现

运算符重载的宏观思考The Complex Class

如何使用运算符重载来简化两个分数的添加?

运算符重载

java实现四则运算