STL set orderinf user defined objets

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
#include<iostream>
using namespace std;
#include<set>

class person{
    int age;
    
    public:
    person(int x)
    {
        age =x;
        
    }
    
    void display() const
    {
        cout<<"Age is "<<age<<endl;
    }
    
    friend bool operator<(person p1,person p2);
    
};

bool operator<(person p1,person p2)
{
     return p1.age>p2.age;
}


int main()
{
     person p[3]={person(25),person(37),person(25)};
     
     set<person>myset(p,p+3);
     
     set<person>::iterator itr;
     
     for(itr=myset.begin();itr!=myset.end();itr++)
     {
         (*itr).display();
     }
     
     return 0;
}

When I ececuted the above code it displays
1
2
Age is 37
Age is 25


Here I have defined operator< so that set can identify the order of person objects.
I am clueless how set could identify that
person p[3]={person(25),person(37),person(25)};
I have provided one duplicated object with age 25

I havent overloaded == operator also,then how it could idetify?
not a<b and not b<a
look at the operator on line 26 compared to the operator on line 24
@ne555

I have got it

@coder777
I was wondering why result was descending :)
Topic archived. No new replies allowed.