zoukankan      html  css  js  c++  java
  • STl 优先队列 C++

    c++中的STL 中的优先队列

    参照网址:

    https://www.cnblogs.com/xzxl/p/7266404.html 代码示例

    https://www.cnblogs.com/zhouzhihao/p/10974594.html 优先队列

    //date:2020.5.2
    #include <bits/stdc++.h>
    using namespace std;
    //重载运算符
    struct cmp1
    {
        bool operator()(int &a,int &b)
        {
            return a>b;//最小值优先
        }
    };
    struct cmp2
    {
        bool operator()(int &a,int &b)
        {
            return a<b;//最小值优先
        }
    };
    struct number1
    {
        int x;
        bool operator< (const number1 &a)const
        {
            return x>a.x;//最小值优先
        }
    };
    struct number2
    {
        int x;
        bool operator <(const number2 &a)const
        {
            return x<a.x;//最大值优先
        }
    };
    int a[]= {14,10,56,7,83,22,36,91,3,47,72,0};
    number1 num1[]= {14,10,56,7,83,22,36,91,3,47,72,0};
    number2 num2[]= {14,10,56,7,83,22,36,91,3,47,72,0};
    int main()
    {
        priority_queue<int>que;
    
        priority_queue<int,vector<int>,cmp1>que1;
        priority_queue<int,vector<int>,cmp2>que2;
    
        priority_queue<int,vector<int>,greater<int> >que3;
        priority_queue<int,vector<int>,less<int> >que4;
    
        priority_queue<number1>que5;
        priority_queue<number2>que6;
    
        int i;
        for(i=0; a[i]; i++)
        {
            que.push(a[i]);
            que1.push(a[i]);
            que2.push(a[i]);
            que3.push(a[i]);
            que4.push(a[i]);
        }
        for(i=0; num1[i].x; i++)
            que5.push(num1[i]);
        for(i=0; num2[i].x; i++)
            que6.push(num2[i]);
    
    
        printf("采用默认优先关系:
    (priority_queue<int>que;)
    ");
        printf("Queue 0:
    ");
        while(!que.empty())
        {
            printf("%3d",que.top());
            que.pop();
        }
        puts("");
        puts("");
    
        printf("采用结构体自定义优先级方式一:
    (priority_queue<int,vector<int>,cmp>que;)
    ");
        printf("Queue 1:
    ");
        while(!que1.empty())
        {
            printf("%3d",que1.top());
            que1.pop();
        }
        puts("");
        printf("Queue 2:
    ");
        while(!que2.empty())
        {
            printf("%3d",que2.top());
            que2.pop();
        }
        puts("");
        puts("");
        printf("采用头文件"functional"内定义优先级:
    (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
    ");
        printf("Queue 3:
    ");
        while(!que3.empty())
        {
            printf("%3d",que3.top());
            que3.pop();
        }
        puts("");
        printf("Queue 4:
    ");
        while(!que4.empty())
        {
            printf("%3d",que4.top());
            que4.pop();
        }
        puts("");
        puts("");
        printf("采用结构体自定义优先级方式二:
    (priority_queue<number>que)
    ");
        printf("Queue 5:
    ");
        while(!que5.empty())
        {
            printf("%3d",que5.top());
            que5.pop();
        }
        puts("");
        printf("Queue 6:
    ");
        while(!que6.empty())
        {
            printf("%3d",que6.top());
            que6.pop();
        }
        puts("");
        return 0;
    }

    输出结果

    采用默认优先关系:
    (priority_queue<int>que;)
    Queue 0:
     91 83 72 56 47 36 22 14 10  7  3
    
    采用结构体自定义优先级方式一:
    (priority_queue<int,vector<int>,cmp>que;)
    Queue 1:
      3  7 10 14 22 36 47 56 72 83 91
    Queue 2:
     91 83 72 56 47 36 22 14 10  7  3
    
    采用头文件"functional"内定义优先级:
    (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
    Queue 3:
      3  7 10 14 22 36 47 56 72 83 91
    Queue 4:
     91 83 72 56 47 36 22 14 10  7  3
    
    采用结构体自定义优先级方式二:
    (priority_queue<number>que)
    Queue 5:
      3  7 10 14 22 36 47 56 72 83 91
    Queue 6:
     91 83 72 56 47 36 22 14 10  7  3
    

      

    四种运算符重载的方法

    struct node{
        int x;
        int y;
        friend bool operator<(const node a,const node b)
        {
            return a.x>b.x;
        }
    };
    priority_queue<node> Q;
    struct node{
        int x;
        int y;
        bool operator<(const node &a) const
        {
            return x>a.x;
        }
    };
    priority_queue<node> Q;
    struct node{
        int x;
        int y;
    }point;
    bool operator<(const node &a,const node &b)
    {
        return a.x>b.x;
    }
    priority_queue<node> Q;
    struct node{
        int x;
        int y;
    };
    struct cmp{
        bool operator()(node a,node b){
            return a.x>b.x;
        }
    };
    priority_queue<node,vector<node>,cmp> Q;
  • 相关阅读:
    数据挖掘实践(34):实战--高潜用户购买画像(三)特征工程
    数据挖掘实践(33):实战--高潜用户购买画像(二)EDA/探索性数据分析
    数据挖掘实践(32):实战--高潜用户购买画像(一)数据清洗
    Java 流程控制 之 顺序结构
    Java 之 运算符
    Java 之 变量
    Handmade Rust Part 1: Introduction & Allocators
    rust 强制转换
    引用与借用
    candidate #1: `std::clone::Clone`
  • 原文地址:https://www.cnblogs.com/someonezero/p/12815766.html
Copyright © 2011-2022 走看看