c++中结构体sort()排序
//添加函数头
#include <algorithm>
//定义结构体Yoy
typedef struct
{
double totalprice; //总价
double storage; //库存
double averageprice; //平均价格
}Toy;
Toy toy[1000];
//定义排序法则
bool compare(Toy a,Toy b)
{
return a.averageprice >b.averageprice ;
} //< 升序 >降序
bool compare1(Toy a,Toy b)
{
if(a.totalprice!=b.totalprice)
{
return a.totalprice<b.totalprice;
}else if(a.storage!=b.storage)
{
return a.storage<b.storage
}
int main()
{
......
sort(toy,toy+N,compare);
......
}