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

posted @ 2018-12-17 11:38  kuaqi  阅读(142)  评论(0编辑  收藏  举报