c++ less operator overload, which way to use?

后端 未结 3 1314
时光取名叫无心
时光取名叫无心 2021-02-12 13:25

For example: in a C++ header file, if I defined a struct Record and I would like to use it for possible sorting so that I want to overload the less operator

3条回答
  •  [愿得一人]
    2021-02-12 13:49

    The best way to define the less operator is:

    struct Record{
        (...)
        const bool operator < ( const Record &r ) const{
            return ( num < r.num );
        }
    };
    

提交回复
热议问题