sort/map/unordered_mapカスタムタイプ比較関数の構築方法

9199 ワード

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