• SET容器


    1、列表格式

    set<class A, class B> colls;
    B的operator()(const B& b1, const B& b2)指定排序方法;

    set<class A>
    不定义排序方法,默认;
    在A中定义排序方法,operator()()中指定排序方法;
    2、基本操作

    3、遍历

    set<Person, PersonCriterion> colls;
    for (auto iter:colls ){ Person p = (Person)(iter); cout << p.FirstName << " " << p.LastName << endl; }
    class Person
    {
    public:
        Person();
    //    ~Person();
        Person(string _firstname, string _lastname)
        {
            this->FirstName = _firstname;
            this->LastName = _lastname;
        }
            public:
        string FirstName;
        string LastName;
        // additional information
    };
    
    class PersonCriterion
    {
    public:
        bool operator()(const Person& p1, const Person& p2)
        {
            return p1.LastName < p2.LastName || (!(p2.LastName<p1.LastName) && p1.FirstName<p2.FirstName);//升序排列
        };
    };
    int main(int argc, char* argv[])
    {
        Person  Person1("Hamberger", "Tom");
        set<Person, PersonCriterion> colls;
        colls.insert(Person("Tom", "James"));
        colls.insert(Person("Mike", "James"));
        colls.insert(Person("Jane", "Bush"));
        colls.insert(Person("Bill", "Gates"));
        std::set<Person, PersonCriterion>::iterator iter;
        for (iter = colls.begin(); iter != colls.end(); ++iter)
        {
            Person p = (Person)(*iter);//获取排序后元素的方式
            cout << p.FirstName << "  " << p.LastName << endl;
        }
        getchar();
        return 1;
    }

    4、排序方法

    1) 元素不是结构体,那么可以编写比较函数

    #include <iostream>  
    #include <set>  
    #include <iterator>  
    using namespace std;  
    
    //自定义比较函数myComp,重载操作符 ()  
    struct myComp  
    {  
        bool operator() (const int &a, const int &b)  
        {  
            return a > b;    //从大到小排序  
            //return a < b;  //从小到大排序  
        }  
    };  
    
    int main()  
    {  
        set<int, myComp> s1;  
        for(int i = 1; i < 6; i++)  
            s1.insert(i*i);  
        s1.insert(8);  
        //ostream_iterator<int> output(cout, " ");  
        set<int, myComp>::iterator it = s1.begin();  
        for(; it != s1.end(); it++)  
        {  
            cout<<*it<<" ";  
        }  
    
        return 0;  
    }  
    
    https://blog.csdn.net/weixin_38391092/article/details/79752018 
    View Code

    2) 元素是结构体,可以直接把比较函数写在结构体内

    #include <iostream>  
    #include <set>  
    #include <iterator>  
    #include <string>  
    using namespace std;  
    
    struct Info  
    {  
        string name;  
        float score;  
        //重载操作符<,自定义排序规则  
        bool operator< (const Info &a)const  
        {  
            //return a.score < score;    //按score由大到小排列  
            return a.score > score;  //由小到大排列  
        }  
    };  
    
    int main()  
    {  
        set<Info> s;  
        Info info;  
        info.name = "Messi";  
        info.score = 8.5;  
        s.insert(info);  
        info.name = "Ronae";  
        info.score = 9.0;  
        s.insert(info);  
        info.name = "My";  
        info.score = 7.0;  
        s.insert(info);  
        info.name = "Perno";  
        info.score = 8.0;  
        s.insert(info);  
        info.name = "Arzar";  
        info.score = 8.5;  
        s.insert(info);  
        set<Info>::iterator it;  
        for(it = s.begin(); it != s.end(); it++)  
        {  
            cout<<it->name<<" : ";  
            cout<<(*it).score<<endl;  
        }  
            return 0;  
    }  
    
    https://blog.csdn.net/weixin_38391092/article/details/79752018 
    View Code

    5、重定义排序方法

    https://www.cnblogs.com/litaozijin/p/6665595.html

  • 相关阅读:
    linux下php环境的装配以及php storm的链接
    p4 : a problem about "./behavioral-model"
    p4factory下 targets/basic_rout
    P4安装
    第二次结对编程作业——毕业导师智能匹配
    初识GIT
    结对项目之需求分析与原型设计
    调研《构建之法》指导下的全国高校的历届软工实践作品、全国互联网+竞赛、物联网竞赛等各类全国性大学生信息化相关的竞赛平台的历届作品及其看法
    SDN 收集一下最近的资料
    软件工程的实践项目课程的自我目标
  • 原文地址:https://www.cnblogs.com/wllwqdeai/p/10130562.html
Copyright © 2020-2023  润新知