• c/c++ 标准库 set 自定义关键字类型与比较函数


    标准库 set 自定义关键字类型与比较函数

    问题:哪些类型可以作为标准库set的关键字类型呢???

    答案:

    1,任意类型,但是需要额外提供能够比较这种类型的比较函数。

    2,这种类型实现了 < 操作。

    答案1的详细说明:声明set时,除了给出元素类型外,还需要给出一个比较函数的类型,注意是类型,不是变量

    方式1:使用decltype,注意后面必须有*

    multiset<Book, decltype(compareIsbn)*> bookstore(compareIsbn);//compareIsbn是实际存在的函数名
    

    方式2:直接使用函数指针

    multiset<Book, bool (*)(const Book &, const Book &)> bookstore(compareIsbn);//compareIsbn是实际存在的函数名
    

    代码块索引:

    代码块 功能描述
    test1 对应上面的答案1
    test2 对应上面的答案2

    例子:

    #include <iostream>
    #include <map>
    #include <unordered_map>
    #include <set>
    #include <unordered_set>
    #include <vector>
    
    using namespace std;
    
    class Book{
    public:
      Book(string bn = "") : isbn(bn){}
      const string& getIsbn() const{
        return isbn;
      }
    private:
      string isbn;
    };
    bool compareIsbn(const Book &b1, const Book &b2){
      return b1.getIsbn() < b2.getIsbn();
    }
    class Student{
    public:
      Student(string n = "", int a = 0) : name(n), age(a){}
      bool operator < (const Student &s) const{
        return age < s.age;
      }
    public:
      string name;
      int age;
    };
    int main(){
      //test1 自定义关键字类型,函数方式                                   
      /* 
      //传递函数指针的第一种写法,使用decltype
      //multiset<Book, decltype(compareIsbn)*>                                      
      //  bookstore(compareIsbn);
      //传递函数指针的第二种写法,直接使用函数指针
      //注意:尖括号里要的是类型,不可以先定义一个函数指针的变量,然后把这个变量放到尖括号里,切记!!!
      multiset<Book, bool (*)(const Book &, const Book &)>                          
        bookstore(compareIsbn);                                                     
      vector<Book> books;                                                           
      for(char c = '5'; c != '1'; --c){                                             
        string tmp = "isbn_0";                                                      
        tmp.insert(tmp.size(), 1, c);                                               
        books.push_back(Book(tmp));                                                 
      }                                                                             
      for(auto const &s : books){                                                   
        cout << s.getIsbn() << " ";                                                 
      }                                                                             
      cout << endl;                                                                 
      bookstore.insert(books.cbegin(), books.cend());                               
      for(auto const &s : bookstore){                                               
        cout << s.getIsbn() << " ";                                                 
      }                                                                             
      cout << endl;                                                                 
      */
    
      //test2 自定义关键字类型,重载<方式                                  
      multiset<Student> students;
      Student s1("C", 3);
      Student s2("A", 5);
      Student s3("A", 4);
      students.insert(s1);
      students.insert(s2);
      students.insert(s3);
      for(auto const &s : students){
        cout << s.name << ": " << s.age << endl;
      }
    }
    

    代码下载

    c/c++ 学习互助QQ群:877684253

    本人微信:xiaoshitou5854

  • 相关阅读:
    【信仰充值中心】Pale Moon 29 正式版更新日志
    【亚伦博客】我们勇敢的审查者
    狐吧吧刊——遇见最好的浏览器(16期)
    【信仰充值中心】Firefox 97 后续更新一览
    JavaTPoint Web 中文教程【翻译完成】
    声纹识别
    设计模式
    WSL迁移安装目录
    openvSwitch概述
    进程间通信几种方式
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9689723.html
Copyright © 2020-2023  润新知