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

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

bool compare(const) const

比较类的定义

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

hash函数的定义

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

具体实现代码

    #include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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& 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 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(mapint, 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
    mapint, cmpMap> studentMap;
    for (int i = 0; i < 10; i++){
        student s("abc", rand());
        studentMap.insert(mapint>::value_type(s, rand()));
    }
    outputMap(studentMap);
}


struct hashKey
{
    size_t operator()(const student& s) const {
        return s.age;
    }
};

void ouputOrderedMap(unordered_mapint, hashKey>& orderedMap)
{
    for (auto tmp : orderedMap) {
        cout.width(10);
        cout.setf(cout.left);
        cout << tmp.first.age << ' ';
    }
    cout << endl;
}


void proceessOrderedMap()
{
    unordered_mapint, 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;
}

你可能感兴趣的:(C++)