当前位置:   article > 正文

C++list容器实现按照年龄升序排列,年龄相同按照身高降序排列,年龄身高相同对体重升序排列_c++ 先按身高排序 再按体重排序

c++ 先按身高排序 再按体重排序

感谢黑马先生!

//对人的年龄进行升序排列,年龄相同对身高进行降序排列,年龄身高相同对体重升序排列
#include <iostream>
#include <list>
#include <string>

using namespace std;


class Person
{
public:
	Person( string Name, int Age,int Heigh,int weight)
	{
		this->m_Name = Name;
		this->m_Age = Age;
		this->m_Heigh = Heigh;
		this->m_weight = weight;
	}
	string m_Name;
	int m_Age;
	int m_Heigh;
	int m_weight;
};

bool Compare(Person& p1, Person& p2)
{
	if (p1.m_Age == p2.m_Age)
	{
		if (p1.m_Heigh == p2.m_Heigh)
		{
			return p1.m_weight < p2.m_weight;
		}
		else
		{
			return p1.m_Heigh > p2.m_Heigh;
		}
	}
	else
	{
		return p1.m_Age < p2.m_Age;
	}
}
void test01()
{
	list<Person> l;
	Person p1 = { "张飞", 23, 170 ,74};
	Person p2 = { "关羽", 23, 170 ,80};
	Person p3 = { "赵云", 23, 180 ,78};
	Person p4 = { "刘备", 25, 199 ,90};
	
	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	l.push_back(p4);

	cout << "-----------------------" << endl;
	for (list<Person>::iterator i = l.begin(); i != l.end(); i++)
	{
		cout << "姓名:" << i->m_Name << " " << "年龄:" << i->m_Age << " " << "身高:" << i->m_Heigh 
			<< "体重:" << i->m_weight <<endl;
	}
	cout << "-----------------------" << endl;
	l.sort(Compare);
	for (list<Person>::iterator i = l.begin(); i != l.end(); i++)
	{
		cout << "姓名:" << i->m_Name << " " << "年龄:" << i->m_Age << " " << "身高:" << i->m_Heigh 
		<< "体重:" << i->m_weight << endl;
	}
}
int main()
{
	test01();
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/442178
推荐阅读
相关标签
  

闽ICP备14008679号