struct heat {
heat(double a, double b, double c) :h1(a), h2(b), h3(c) {}
double h1;
double h2;
double h3;
};
bool comp(heat a, heat b)
{
if (fabsf(a.h1 - b.h1) > 0.000001)
{
if (a.h1 - b.h1 > 0.000001)
return true;
else
return false;
}
else if (fabsf(a.h2 - b.h2) > 0.000001)
{
if (a.h2 - b.h2 > 0.000001)
return true;
else
return false;
}
else
{
if (a.h3 - b.h3 > 0.000001)
return true;
else
return false;
}
}
验证:
std::vector<heat> h;// ({ 0.8,0.7,0.6 }, { 0.8,0.75,0.6 }, { 0.9,0.7,0.6 });
heat tmp1(0.8, 0.7, 0.6);
heat tmp2(0.8, 0.75, 0.6);
heat tmp3(0.9, 0.7, 0.6);
h.push_back(tmp1);
h.push_back(tmp2);
h.push_back(tmp3);
std::sort(h.begin(), h.end(), comp);
注意:c++ 为什么std :: sort compare函数必须在参数相等时返回false?