当前位置: 移动技术网 > IT编程>开发语言>C/C++ > c/c++ 标准库 set 自定义关键字类型与比较函数

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

2018年09月23日  | 移动技术网IT编程  | 我要评论

李白的古诗大全,王一婷,赵四搞笑视频

标准库 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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网