Creating a composite type from two enum classes, ready for STL map

后端 未结 6 1699
星月不相逢
星月不相逢 2021-02-14 05:26

I would like to create a composite type out of two enum classes.

enum class Color {RED, GREEN, BLUE};
enum class Shape {SQUARE, CIRCLE, TRIANGLE};

         


        
6条回答
  •  醉话见心
    2021-02-14 05:47

    You only need to compare shape if color is the same for both.

    Using a ternary you can make it look nice too:

    friend bool operator< (const Object &lhs, const Object &rhs) {
         return lhs.color == rhs.color ? (lhs.shape < rhs.shape)
                                       : (lhs.color < rhs.color);
    }
    

提交回复
热议问题