• 设计模式 C++迭代器模式


    // sejimoshi.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    
    #include <iostream>
    #include <list>
    #include<algorithm>
    using namespace std;
    
    class Student
    {
    private:
        string name;
        long id;
        int age;
    public:
        string getName() {
            return name;
        }
        long getId() {
            return id;
        }
        int getAge() {
            return age;
        }
        void setName(string name) {
            this->name = name;
        }
        void setAge(int age) {
            this->age = age;
        }
        void setId(long id) {
            this->id = id;
        }
        Student(string name, long id, int age) {
            this->name = name;
            this->id = id;
            this->age = age;
        }
    };
    
    
    //比较函数:根据结构体里面的整型number排序
    bool sortStuInt(Student m1, Student m2)
    {
        return m1.getId() < m2.getId();
    }
    //比较函数:根据结构体里面的字符串name排序
    bool comStuString(Student m1, Student m2)
    {
        if (m1.getId() > m2.getId())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    int main()
    {
        std::cout << "Hello World!\n";
        list<Student> list_student;
        list_student.push_back(Student("lisi", 20194001, 20));
        list_student.push_back(Student("wangwu", 20194002, 21));
        list_student.push_back(Student("zhaosi", 20194003, 22));
        list_student.push_back(Student("xiaoming", 20194005, 20));
        list_student.push_back(Student("xiaohong", 20194004, 22));
        list_student.push_back(Student("xiaohuang", 20194010, 20));
        list_student.push_back(Student("xiaolan", 20194008, 20));
        cout << "迭代器正序遍历\n";
        list_student.sort(sortStuInt);
        //使用begin()/end()迭代器函数对输出list容器中的元素
        for (std::list<Student>::iterator it = list_student.begin(); it != list_student.end(); ++it) {
            std::cout << (*it).getId()<<endl;
        }
        cout << "迭代器倒序遍历\n";
        list_student.sort(comStuString);
        //使用begin()/end()迭代器函数对输出list容器中的元素
        for (std::list<Student>::iterator it = list_student.begin(); it != list_student.end(); ++it) {
            std::cout << (*it).getId() << endl;
        }
    }
    
    bool cmp_1(Student x, Student y) {
        return x.getId() > y.getId();
    }
    bool cmp_2(Student x, Student y) {
        return x.getId() < y.getId();
    }
  • 相关阅读:
    大组合取模之:1<=n<=m<=1e6,1<=p<=1e9
    大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数
    fzu2020( c(n,m)%p,其中n, m, p (1 <= m <= n <= 10^9, m <= 10^4, m < p < 10^9, p是素数) )
    lucas定理证明
    各类小公式
    x^a=b(mod c)求解x在[0,c-1]上解的个数模板+原根求法
    快速幂+乘模 模板
    hdu1695(容斥 or 莫比乌斯反演)
    poj1845(二分快速求等比数列模M和)
    2018JAVA面试题附答案
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/15559632.html
Copyright © 2020-2023  润新知