1. 程式人生 > >sort/map/unordered_map自定義型別如何構造比較函式

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 <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; }