UNABLE TO UNDERSTAND THE OPERATOR

Sir for A.age<B.age.Output should be persons having lesser age the highest priority.But its coming out to be age in descending order.
#include
#include
#include
using namespace std;

class Person
{
public:

string name;
int age;
  
  Person()
  {
      
  }

  Person(string a,int b)
  {
      name=a;
      age=b;
  }

};

class PersonCompare
{
public:

  bool operator()(Person A,Person B)
  {
      return A.age<B.age;
  }

};

int main()
{
int n;
cin>>n;

priority_queue<Person,vector,PersonCompare> pq;

for(int i=0;i<n;i++)
{
string name;
int age;
cin>>name>>age;
Person p(name,age);
pq.push§;
}

int k=3;
for(int i=0;i<k;i++)
{
Person p=pq.top();
cout<<p.name<<" "<<p.age<<endl;
pq.pop();
}
}

@Mukul-Shane-1247687648773500 no that will come with a.age > b.age

Sir for a.age<b.age it is giving the top most person with highest age instead of lowest age.

@Mukul-Shane-1247687648773500 no it will give that only for increasing order u need to change it to >

#include #include #include using namespace std; class Person { public: string name; int age; Person() { } Person(string a,int b) { name=a; age=b; } }; class PersonCompare { public: bool operator()(Person A,Person B) { return A.age>B.age; } }; int main() { int n; cin>>n; priority_queue<Person,vector,PersonCompare> pq; for(int i=0;i<n;i++) { string name; int age; cin>>name>>age; Person p(name,age); pq.push§; } cout<<endl; int k=3; for(int i=0;i<k;i++) { Person p=pq.top(); cout<<p.name<<" "<<p.age<<endl; pq.pop(); } }

// input 5 a 30 b 9 c 60 d 5 e 78 // output d 5 b 9 a 30

Sir for greater than output is in ascending order.Please check it.

hi @Mukul-Shane-1247687648773500 please share the code in ide.codingblocks.com

hi @Mukul-Shane-1247687648773500 no that doent work this way
refer
https://www.geeksforgeeks.org/comparator-class-in-c-with-examples/#:~:text=The%20comparator%20class%20compares%20the,%2C%20otherwise%2C%20it%20returns%20false.

Sir here also harsh it has lowest roll num and therefore his name comes out on top for <.But for my code its coming in descending order.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.