当前位置: 首页 > article >正文

C++,STL 042(24.10.21)

内容

一道练习题。

(涉及list,sort)

题目(大致)

将Person自定义类型进行排序(Person中属性有姓名、年龄、身高),按照年龄进行升序,如果年龄相同则按照身高进行降序。

运行代码

#include <iostream>
#include <string>
#include <list>

using namespace std;

class Person
{
public:
    string m_Name;
    int m_Age;
    int m_Height;

public:
    Person(string name, int age, int height)
    {
        this->m_Name = name;
        this->m_Age = age;
        this->m_Height = height;
    }
};

bool comparePerson(Person &p1, Person &p2)
{
    if (p1.m_Age == p2.m_Age)
    {
        return p1.m_Height > p2.m_Height;
    }
    else
    {
        return p1.m_Age < p2.m_Age;
    }
}

void test01()
{
    list<Person> l;

    Person p1("p1", 23, 166);
    Person p2("p2", 23, 156);
    Person p3("p3", 23, 178);
    Person p4("p4", 33, 172);
    Person p5("p5", 43, 190);
    Person p6("p6", 45, 175);

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

    cout << "排序前:" << endl;
    for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
    {
        cout << it->m_Name << " " << it->m_Age << " " << it->m_Height << endl;
    }

    l.sort(comparePerson);

    cout << "排序后:" << endl;
    for (list<Person>::iterator it = l.begin(); it != l.end(); it++)
    {
        cout << it->m_Name << " " << it->m_Age << " " << it->m_Height << endl;
    }
}

int main()
{
    test01();

    return 0;
}

输出结果


http://www.kler.cn/news/364809.html

相关文章:

  • uniapp 常用的地区行业各种多选多选,支持回显,复制粘贴可使用
  • Discuz发布原创AI帖子内容生成:起尔 | AI原创帖子内容生成插件开发定制
  • 技术成神之路:二十三种设计模式(导航页)
  • Unsupervised Domain Adaptation in SemanticSegmentation: A Review——论文笔记
  • OQE-OPTICAL AND QUANTUM ELECTRONICS
  • 使用 Python 和 Pandas 处理 Excel 数据:合并单元格示例
  • 探索Web3与区块链的融合:未来互联网的新范式
  • Linux的用户管理、组帐号管理(chmod,chown)
  • Makefile:1954: recipe for target ‘Modules/_ssl.o‘ failed请安装ssl后,重新安装或编译Python
  • VS2022控制台程序显示命名空间引用
  • 影刀RPA实战:网页爬虫之桌面壁纸图片
  • 闯关leetcode——168. Excel Sheet Column Title
  • CSV文件自动化生成:用Pandas与Datetime高效处理商品信息
  • 【matlab 计算任意两个序列的dtw距离】
  • CG-66无线土壤水分传感器,实时在线测量土壤温湿度,并使用物联网进行传输
  • 嵌入式面试刷题(day19)
  • 从零开始的LeetCode刷题日记:55. 跳跃游戏
  • 全面了解MindSporeLite轻量化推理工具(概念版)
  • 企业内部知识库管理系统,nlp,知识图谱,全文检索的知识库源码
  • 数据挖掘:基于电力知识图谱的客户画像构建实施方案
  • Python os模块详解
  • 开源运维软件适用性评估:多维度视角下的理性选择
  • 【python_修改PPT中字体,run.font.name只对英文生效怎么办?】
  • 告别繁琐操作!一文教你轻松做出高效报表
  • ETCD未授权访问风险基于角色认证和启用https的ca证书修复方案
  • Vue学习笔记(二、Vue.js的引入与对象创建)