使用struct作为std::map的key值,并支持比较

struct定义

struct keyStruct {
	int a, b, c;
	bool operator < (const keyStruct& ks) const {
	   if (a == ks.a &&
	       b == ks.b &&
	       c == ks.c) {
	          return true;
	       }
	    return false;
	}
};

这样存在问题,当构造map时,始终只有一个std::pair在map中,因为有序map不光需要比较等于,还需要比较顺序,a,b,c不同的大小关系来排树形结构。
一种方法是使用unorder map,只需要提供operator()重载。

正确的方式是使用tuple:

#include 

bool operator < (const keyStruct& ks) const {
	return std::tie(a,b,c) < std::tie(ks.a, ks.b, ks.c);
}

你可能感兴趣的:(算法,数据结构)