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

后端 未结 6 1790
星月不相逢
星月不相逢 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:41

    You don't need a linear index, you can simply compare them lexicographically:

    friend bool operator< (const Object &lhs, const Object &rhs) {
      if (lhs.color < rhs.color) return true;
      else if (lhs.color > rhs.color) return false;
      else return lhs.shape < rhs.shape;
    }
    

提交回复
热议问题