#include<iostream>
usingnamespace std;
#include<set>
class person{
int age;
public:
person(int x)
{
age =x;
}
void display() const
{
cout<<"Age is "<<age<<endl;
}
friendbooloperator<(person p1,person p2);
};
booloperator<(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?