在manager类中,需要将lc与le中的元素按所付金额的大小排队,但无法直接用sort为list排序,因为list中的元素都是customer与employee类型,无法用>比较,必须重新定义比较函数。
bool comparecustomer(const Customer& a, const Customer& b)
{
return (a.get_cash() > b.get_cash());
}
void Manager::calculate()
{
lc.sort(comparecustomer);
//将排序后的lc依次输出
}
这样便可根据顾客所付金额的多少排序了。
本文介绍了一种在C++中对包含Customer与Employee类型的列表进行排序的方法,通过自定义比较函数comparecustomer,根据Customer对象的get_cash()方法返回的金额大小进行排序。

被折叠的 条评论
为什么被折叠?



