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();
}
[C/C++]map自定义比较函数
最新推荐文章于 2024-06-11 20:46:21 发布