zoukankan      html  css  js  c++  java
  • 队列 && 栈

    队列:

    #include <queue>

    queue<int> Q;

    (1)Q.front();  取队头

    (2)Q.back();   取队尾

    (3)Q.pop();    删队头

    (4)Q.push();   队尾加元素

    (5)Q.size();   队列实际个数

    (6) Q.empty(); 判断是否为空

    备注:Q是队列名

     

    优先队列:

    #include <queue>

    priority_queue <int> Q;

    1.empty( )  //判断一个队列是否为空

    2.pop( )  //删除队顶元素

    3.push( )  //加入一个元素

    4.size( )  //返回优先队列中拥有的元素个数

    5.top( )  //返回优先队列的队顶元素

     另外,优先队列的默认优先级是从大到小(即大根堆),如果想改为从小到大,就需要自定义优先级。(如下:)

    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <queue>
    using namespace std;
    struct point
    {
        int x,y,times;
        bool operator < (const point &a)const //重载为小根堆 
        {
            return a.times<times;
        }
    };
    int main()
    {
        priority_queue <int,vector<int>,greater<int> > Q; //不管输入几个数都这么写,别忘了<>里面最后要加一个空格 
        for(int i=4;i>=1;i--) Q.push(i);
        for(int i=1;i<=4;i++)
        {
            printf("%d",Q.top());
            Q.pop();
        }
        system("pause");
        return 0;
    }

     

    栈:

    #include <stack> 
    stack <int> s;
    1. s.top();       取栈顶元素
    2. s.pop();      删除栈顶元素
    3. s.push();    把自变量压入栈顶(进栈)
    4. s.empty();  判断栈是否为空 (0/1)
    5. s.size();     返回栈的实际应用个数
    备注:s是栈名

  • 相关阅读:
    vscode调试pomelo和pomelo使用vscode调试
    linux修改单个进程的系统时间
    python之路目录
    python之前端
    python之ORM操作
    python之redis和memcache操作
    python之消息队列
    python之协程与IO操作
    python之进程与线程
    python之网络编程
  • 原文地址:https://www.cnblogs.com/YXY-1211/p/5160192.html
Copyright © 2011-2022 走看看