【转】 自定义结构体作为map的key

 1 /**
 2  * self defined struct as the key of map in c++
 3  */
 4 struct K {
 5     int n1, n2;
 6     K(int i, int j): n1(i), n2(j) {}
 7     // the operator < defines the operation used in map
 8     friend bool operator < (const struct K &k1, const struct K &k2);
 9 };
10  
11 inline bool operator < (const struct K &k1, const struct K &k2) {
12     return k1.n1 < k2.n1 || (k1.n1==k2.n1 && k1.n2<k2.n2);
13 }

参考链接:https://blog.csdn.net/yangyangye/article/details/85099842

posted @ 2020-08-11 19:45  gooneybird  阅读(506)  评论(0)    收藏  举报