结构体运算符重载和优先队列的优先级

结构体运算符重载和优先队列的优先级 

#include
#include
#include

using namespace std;

struct node  
{  
    friend bool operator< (node n1, node n2)  
    {  
        return n1.priority > n2.priority;  //"<"为从大到小排列,">"为从小到大排列  
    }  
    int priority;  
    int value;  
};  
int main()
{
	const int len =5;
	priority_queue qn;  //必须要重载运算符  
    node b[len];  
    b[0].priority = 6; b[0].value = 1;  
    b[1].priority = 9; b[1].value = 5;  
    b[2].priority = 2; b[2].value = 3;  
    b[3].priority = 8; b[3].value = 2;  
    b[4].priority = 1; b[4].value = 4;  
   
    for(int i = 0; i < len; i++)  
        qn.push(b[i]);  
    cout<<"优先级"<<'\t'<<"值"<

其中qn.push(b[i]    b[i]相当于结构体变量,添加到队列里,因此qn.top().priotity队首元素是结构体变量访问其成员。队列按照priority自动进行排序(不是按照value来排序的)

node类型和int类型不一样

你可能感兴趣的:(结构体运算符重载和优先队列的优先级)