Help in Template in priority queue

Hi. i want to pass to a priority queue some structs.
for example:
1
2
3
4
5
6
7
struct person
{
int age;
char sex;
sting name;
//etc
};

I want to push this. I want the compare to be first women and if 2 persons are women take the one with the smallest age. Can you help me write the template?
thank you so much
Create a person comparison function

something like this (code not tested)

1
2
3
4
5
6
7
8
9
10
11
12
13
class personcompare
{
public:
  bool operator() (const person& p1, const person& p2) const
  { // largest elements will be at top of priority queue
     if(p1.sex == 'F' && p2.sex == 'M')
       return true;
     if(p1.sex == 'M' && p2.sex == 'F')
       return false;
     // got to here then they are the same sex - youngest first ("largest" element)
     return(p1.age > p2.age);
  }
};


create a priority_queue

 
priority_queue<person,vector<person>,personcompare> Q;


Topic archived. No new replies allowed.