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

    为了明天的讲课  今天一直在看广搜  队列  优先队列   算是自己又多了点知识  学长说看我紧张成这样   好吧他明天讲吧   啊  心一下放松了   

    今天死磕优先队列了   好吧  来写一下总结

    优先队列   我认为就是从一堆数据中挑优先级别高的出队   并不是像对列一样先进先出

    优先级的大小是自己定义的    operator是一个重载 

    比如我现在想让x小的先出队   我就可以这样写

    struct node

    {

          int x,y;

          bool operator(const node &n1)const

         {

                 return x>n1.x;

         }

    }

    或者

    struct node

    {

          int x,y;

          friend bool operatop(const node n1,const node n2)

         {

                 returen n1.x>n2.x;

         }

    }

    运用优先队列    就是    priority_queue<node>Q;

    优先队列  就像一个cmp差不多   可以直接用sort  排序结构体 

    比如:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<iostream>
    #include<algorithm>
    #include<math.h>
    #include<queue>
    #define N 220
    using namespace std;
    struct node
    {
        int x,y;
        bool operator<(node n1)const
        {
            return x>n1.x;
        }
    };
    
    int main()
    {
        node a[N];
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&a[i].x,&a[i].y);
        }
    
        sort(a,a+n);
    
        for(int i=0;i<n;i++)
        {
            printf("%d %d
    ",a[i].x,a[i].y);
        }
        return 0;
    }

    可能还有好多我不知道的功能  今天就先说到这里  我们下周同一时间再见(哈哈)

    以后慢慢发现慢慢更新

  • 相关阅读:
    模块-和包
    re模块
    递归函数
    内置函数
    C++ 创建文件的方法
    C++多态的实现条件
    C++常见错误总结
    Http客户端跳转和服务器端跳转的区别
    struts1学习
    Java 核心读书笔记 第11章
  • 原文地址:https://www.cnblogs.com/linliu/p/5161713.html
Copyright © 2011-2022 走看看