How can I create Min stl priority_queue?

前端 未结 9 848
误落风尘
误落风尘 2020-12-04 04:49

The default stl priority queue is a Max one (Top function returns the largest element).

Say, for simplicity, that it is a priority queue of int values.

9条回答
  •  离开以前
    2020-12-04 05:38

    You can do it in multiple ways:
    1. Using greater as comparison function :

     #include 
    
    using namespace std;
    
    int main()
    {
        priority_queue,greater >pq;
        pq.push(1);
        pq.push(2);
        pq.push(3);
    
        while(!pq.empty())
        {
            int r = pq.top();
            pq.pop();
            cout<

    2. Inserting values by changing their sign (using minus (-) for positive number and using plus (+) for negative number :

    int main()
    {
        priority_queuepq2;
        pq2.push(-1); //for +1
        pq2.push(-2); //for +2
        pq2.push(-3); //for +3
        pq2.push(4);  //for -4
    
        while(!pq2.empty())
        {
            int r = pq2.top();
            pq2.pop();
            cout<<-r<<" ";
        }
    
        return 0;
    }
    

    3. Using custom structure or class :

    struct compare
    {
        bool operator()(const int & a, const int & b)
        {
            return a>b;
        }
    };
    
    int main()
    {
    
        priority_queue,compare> pq;
        pq.push(1);
        pq.push(2);
        pq.push(3);
    
        while(!pq.empty())
        {
            int r = pq.top();
            pq.pop();
            cout<

    4. Using custom structure or class you can use priority_queue in any order. Suppose, we want to sort people in descending order according to their salary and if tie then according to their age.

        struct people
        {
            int age,salary;
    
        };
        struct compare{
        bool operator()(const people & a, const people & b)
            {
                if(a.salary==b.salary)
                {
                    return a.age>b.age;
                }
                else
                {
                    return a.salary>b.salary;
                }
    
        }
        };
        int main()
        {
    
            priority_queue,compare> pq;
            people person1,person2,person3;
            person1.salary=100;
            person1.age = 50;
            person2.salary=80;
            person2.age = 40;
            person3.salary = 100;
            person3.age=40;
    
    
            pq.push(person1);
            pq.push(person2);
            pq.push(person3);
    
            while(!pq.empty())
            {
                people r = pq.top();
                pq.pop();
                cout<
    1. Same result can be obtained by operator overloading :

      struct people
      {
      int age,salary;
      
      bool operator< (const people & p)const
      {
          if(salary==p.salary)
          {
              return age>p.age;
          }
          else
          {
              return salary>p.salary;
          }
      }};
      

      In main function :

      priority_queue pq;
      people person1,person2,person3;
      person1.salary=100;
      person1.age = 50;
      person2.salary=80;
      person2.age = 40;
      person3.salary = 100;
      person3.age=40;
      
      
      pq.push(person1);
      pq.push(person2);
      pq.push(person3);
      
      while(!pq.empty())
      {
          people r = pq.top();
          pq.pop();
          cout<

提交回复
热议问题