zoukankan      html  css  js  c++  java
  • stl之优先队列

    #include<iostream>
    #include<functional>
    #include<queue>
    using namespace std;

    //这是写了一个结构体
    //然后是让它具有一些具体的功能实现,
    //比如说就是下面的排序
    //结果并不是通过两个熟知的大小来进行顺序排列的
    //而是通过自己的定义来实现,像priority这个功能

    struct node {    
    friend bool operator< (node n1, node n2)    
    {        
    return n1.priority < n2.priority;
    //"<"为从大到小排列
    //">"为从小打到排列    
    }    
    int priority;    
    int value;
    };

    int main()
    {    
    const int len = 5;    
    int i;    
    int a[len] = {3,5,9,6,2};    
    //示例1    
    priority_queue<int> qi;//普通的优先级队列,按从大到小排序    
    for(i = 0; i < len; i++)        
    qi.push(a[i]);     //入栈
    for(i = 0; i < len; i++)    
    {        
    cout<<qi.top()<<" ";        
    qi.pop();    
    }    
    cout<<endl;    
    //示例2    
    priority_queue<int, vector<int>, greater<int> > qi2;
    //从小到大的优先级队列,可将greater改为less,即为从大到小    
    for(i = 0; i < len; i++)        
    qi2.push(a[i]);    
    for(i = 0; i < len; i++)    
    {        
    cout<<qi2.top()<<" ";        
    qi2.pop();    
    }     cout<<endl;    
    //示例3    
    priority_queue<node> 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(i = 0; i < len; i++)        
    qn.push(b[i]);    
    cout<<"优先级"<<' '<<"值"<<endl;    
    for(i = 0; i < len; i++)     {        
    cout<<qn.top().priority<<' '<<qn.top().value<<endl;        
    qn.pop();    
    }    
    return 0;

    }

    我要坚持一年,一年后的成功才是我想要的。
  • 相关阅读:
    A Node Influence Based Label Propagation Algorithm for Community detection in networks 文章算法实现的疑问
    Fast Newman-FN算法以及模块度定义介绍
    Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现
    设计一个smartnic
    Intel GEN11 GPU
    Intel GEN9 GPU
    Shared Virtual Memory (SVM) Functions
    connect via ssh to virtualbox guest vm without knowing ip address
    smartnic
    技术精品翻译
  • 原文地址:https://www.cnblogs.com/tianxia2s/p/3881338.html
Copyright © 2011-2022 走看看