C++利用if else做判断最后输出,年龄相同,身高降序,身高相同,体重降序

bool compareperson(person& p1, person& p2)
{
    
    ??????????
    
    
}

void test01()
{

    list<person> l;

    person p1("曹操", 35, 175,140);
    person p2("刘备", 45, 180,150);
    person p3("孙权", 40, 170,140);
    person p4("赵云", 25, 190,160);
    person p5("张飞", 35, 175,180);
    person p6("关羽", 35, 200,170);


    l.push_back(p1);
    l.push_back(p2);
    l.push_back(p3);
    l.push_back(p4);
    l.push_back(p5);
    l.push_back(p6);
    
    for (list<person>::iterator it = l.begin(); it != l.end(); it++)
    {
        cout << "姓名: " << it->mname << "年龄: " << it->mage << "身高: " << it->mheight << "体重:" << it->mweight << endl;
    }
 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class person{
public:
	string mname;
	int mage;
	int mheight;
	int mweight;
	person(std::string name, int age, int height, int weight){
		mname = name;
		mage = age;
		mheight = height;
		mweight = weight;
	}
};
bool compareperson(person& p1, person& p2)
{
	if (p1.mage == p2.mage)
	{
		if (p1.mheight == p2.mheight)
			return p1.mweight > p2.mweight;
		return p1.mheight > p2.mheight;
	}
	else
		return p1.mage > p2.mage;
}

void main()
{
	vector<person> l;

	person p1("曹操", 35, 175, 140);
	person p2("刘备", 45, 180, 150);
	person p3("孙权", 40, 170, 140);
	person p4("赵云", 25, 190, 160);
	person p5("张飞", 35, 175, 180);
	person p6("关羽", 35, 200, 170);


	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	l.push_back(p4);
	l.push_back(p5);
	l.push_back(p6);

	sort(l.begin(), l.end(), compareperson);

	for (vector<person>::iterator it = l.begin(); it != l.end(); it++)
	{
		std::cout << "姓名: " << it->mname << "年龄: " << it->mage << "身高: " << it->mheight << "体重:" << it->mweight << endl;
	}
}

 

list是不可以用sort排序的

要写个排序算法,排好序再输出。

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y