relationship between string bool operator and map container class

Sorry gents but I'm still not clear how the bool operator function relates to the <map> container class. I understand the common element between them is the class X but fail to see how the <map> operates on the bool operator function. The compiler freeks if I remove comment out the bool operator function??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class X
{
    ...
};

class Y
{
    ...
};

bool operator < (const X& first, const X& second)
{
    return first.memberofX < second.memberofX;
}

int main()
{
    map<X, Y> mymap;

    ...
    
    return 0;
}

std::map is an associative array that uses an internal binary tree. It must keep all pairs sorted by the key value. Therefore whatever you use as the key_type, in this case the X type, the std::map must be able to compare instances of type X against one another. You'd have the same problem with the std::set if you wanted to fill it with X objects. Read std::map's documentation more closely and study the examples provided on this site.
Topic archived. No new replies allowed.