1. 程式人生 > >[C/C++]map自定義比較函式

[C/C++]map自定義比較函式

class Stu
{
public:
	Stu(int b = 0):a(b)
	{
	
	}
	/*bool operator < (const Stu& k)const  方法一: 兩個const都需要加上,否則編譯不通過
	{
		return a < k.a;
	}*/
public:
	int a;
};

struct compare
{
	bool operator()(const Stu& a, const Stu& b) //方法二: 兩個const都需要加上,否則編譯不通過
	{
		return a.a < b.a;
	}
};
int main()
{
	
	Stu test1;
	Stu test2;

	map<Stu, int,compare> kk;
	kk.insert(pair<Stu, int>(test1, 1));
	kk.insert(pair<Stu, int>(test2, 2));



	
	getchar();
		

}