设为首页 加入收藏

TOP

c/c++ 标准库 set 自定义关键字类型与比较函数
2018-10-21 14:12:38 】 浏览:42
Tags: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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇笔试题——C++后序字符比较 下一篇cf643E. Bear and Destroying Sub..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目