sort/map/unordered_map自定义类型如何构造比较函数

来源:互联网 发布:命令行启动mysql 编辑:程序博客网 时间:2024/06/02 01:49

sort: 定义比较函数 / 定义比较类,用比较类定义对象
map: 比较类 / 比较函数在自定义类中提供
unordered_map: hash类的定义、 ==运算符重载
注:/代表或, 、代表并
比较函数

bool compare(const) const

比较类的定义

    struct cmp {        bool operator()(const ) const    }; 

hash函数的定义

    struct hashKey {        size_t operator()(const) const     };

具体实现代码

    #include <iostream>#include <random>#include <vector>#include <map>#include <array>#include <unordered_map>#include <algorithm>#include <random>#include <functional>#include <time.h>#include <string>using namespace std;const unsigned arraySize = 10;typedef struct student {    bool operator < (const student& r) const { return age < r.age; }    bool operator==(const student& r) const { return age == r.age && name == r.name; }    student() { }    student(string n, int a) : name(n), age(a) { }    string name;    int age;}student;typedef struct cmpLess{    bool operator()(const student& l, const student& r) const {        return l.age < r.age;    }}cmpLess; /*typedef struct cmpLess cmpLess = struct cmpLess*/void outputArray(array<student, arraySize>& inputArray) {    cout.setf(cout.left);    for (int i = 0; i < arraySize; i++) {        cout.width(6);        cout << inputArray.at(i).age << ' ';    }    cout << endl;}void processArray(){    std::srand(time(NULL)); // use current time as seed for random generator    array<student, arraySize> myArray;    for (int i = 0; i < 10; i++) {        student s("abc", rand());        myArray.at(i) = std::move(s);    }    outputArray(myArray);    /*    three program    *first:     define compare function    *second:    define operator<    *third:     define compare class    */    //sort(myArray.begin(), myArray.end(), [](const student& l, const student& r) {return l.age < r.age;});    //sort(myArray.begin(), myArray.end());    sort(myArray.begin(), myArray.end(), cmpLess());    outputArray(myArray);}struct cmpMap{    bool operator()(const student& l, const student& r) const    { return l.age < r.age;}};void outputMap(map<student, int, cmpMap>& inputMap){    for (auto v : inputMap) {        cout.width(10);        cout.setf(cout.left);        cout << v.first.age << ' ';    }    cout << endl;}void processMap() {    std::srand(time(NULL)); // use current time as seed for random generator    map<student, int, cmpMap> studentMap;    for (int i = 0; i < 10; i++){        student s("abc", rand());        studentMap.insert(map<student, int>::value_type(s, rand()));    }    outputMap(studentMap);}struct hashKey{    size_t operator()(const student& s) const {        return s.age;    }};void ouputOrderedMap(unordered_map<student, int, hashKey>& orderedMap){    for (auto tmp : orderedMap) {        cout.width(10);        cout.setf(cout.left);        cout << tmp.first.age << ' ';    }    cout << endl;}void proceessOrderedMap(){    unordered_map<student, int, hashKey> unorderedMap;    for (int i = 0; i < 10; i++) {        student s("abd", rand()%100);        unorderedMap.insert(make_pair(s, rand()));    }    ouputOrderedMap(unorderedMap);}int main(){    cout << "=====sort======" << endl;    processArray();    cout << "=====map======" << endl;    processMap();    cout << "=====unorderted_map======" << endl;    proceessOrderedMap();    system("pause");    return 0;}