Sorted maps

What I want is a map of classes with string index to be ordered by a member of the class. According to this https://stackoverflow.com/questions/5733254/how-can-i-create-an-own-comparator-for-a-map
I thought this would do it but I am doing something wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <map>

using namespace std;

class aaa
{
protected:
    int value;

public:
    aaa(void)
    { 
        value = 0; 
    };
    
    aaa(int l)
    { 
        value = l; 
    };
    
    int Value(void)
    {
        return value;
    };

    static struct ByValue: public std::binary_function<aaa, aaa, bool>
    {
        bool operator()(const aaa &lhs, const aaa &rhs) const
        {
            return lhs.value < rhs.value;
        }
    };

    static struct ByValue2: public std::binary_function<aaa*, aaa*, bool>
    {
        bool operator()(const aaa *lhs, const aaa *rhs) const
        {
            return lhs->value < rhs->value;
        }
    };

};

void main(void)
{
    //map<string, aaa*, aaa::ByValue2> myMap;
    map<string, aaa, aaa::ByValue> myMap;
    //map<string, aaa> myMap;

    /*
    myMap["a"] = new aaa(6);
    myMap["b"] = new aaa(5);
    myMap["c"] = new aaa(4);
    myMap["d"] = new aaa(3);
    myMap["e"] = new aaa(2);
    myMap["f"] = new aaa(1);
    myMap["g"] = new aaa(0);
    myMap["h"] = new aaa(-1);
    myMap["i"] = new aaa(-2);
    myMap["j"] = new aaa(-3);
    //*/

    //*
    myMap["a"] = aaa(6);
    myMap["b"] = aaa(5);
    myMap["c"] = aaa(4);
    myMap["d"] = aaa(3);
    myMap["e"] = aaa(2);
    myMap["f"] = aaa(1);
    myMap["g"] = aaa(0);
    myMap["h"] = aaa(-1);
    myMap["i"] = aaa(-2);
    myMap["j"] = aaa(-3);
    //*/

    for(auto &i : myMap)
    {
        //cout << i.first << ":" << i.second->Value() << endl;
        cout << i.first << ":" << i.second.Value() << endl;
    }

    system("pause");

    /*delete myMap["a"];
    delete myMap["b"];
    delete myMap["c"];
    delete myMap["d"];
    delete myMap["e"];
    delete myMap["f"];
    delete myMap["g"];
    delete myMap["h"];
    delete myMap["i"];
    delete myMap["j"];*/
}
Maps are always ordered by key.
Topic archived. No new replies allowed.