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

    C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。
    1.back() 返回一个引用,指向最后一个元素
    2.empty() 如果队列空则返回真
    3.front() 返回第一个元素
    4.pop() 删除第一个元素
    5.push() 在末尾加入一个元素
    6.size() 返回队列中元素的个数

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<queue>
    #include<algorithm>
    const int maxn=1007;
    const int INF=0x3f3f3f3f;
    using namespace std;
    
    int main()
    {
        queue<int>Q;
        Q.push(1);
        Q.push(5);
        printf("%d
    ", Q.front());
        return 0;
    }

    C++ Priority Queues(优先队列)
    C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。
    1.empty() 如果优先队列为空,则返回真
    2.pop() 删除第一个元素
    3.push() 加入一个元素
    4.size() 返回优先队列中拥有的元素的个数
    5.top() 返回优先队列中有最高优先级的元素

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<queue>
    #include<algorithm>
    const int maxn=1007;
    const int INF=0x3f3f3f3f;
    using namespace std;
    
    priority_queue<int>Q;
    
    int main()
    {
        for(int i=0; i<4; i++)
            Q.push(i);
    
        while(!Q.empty())
        {
            int x=Q.top();
            Q.pop();
            printf("%d
    ", x);
        }
        return 0;
    }

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<queue>
    #include<algorithm>
    const int maxn=1007;
    const int INF=0x3f3f3f3f;
    using namespace std;

    priority_queue<int>Q;

    int main()
    {
    for(int i=0; i<4; i++)
    Q.push(i);

    while(!Q.empty())
    {
    int x=Q.top();
    Q.pop();
    printf("%d ", x);
    }
    return 0;
    }

  • 相关阅读:
    我的浏览器和常用扩展
    Win10安装.Net Framework4.7及更高版本
    压缩和解压工具bandizip
    Oracle trunc()函数的用法
    Oracle 中 decode 函数用法
    Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法
    String literal is not properly closed by a double-quote eclipse
    linux 启动 Oracle 实例
    查询当前Oracle数据库的实例
    sqlplus 执行 sql 文件
  • 原文地址:https://www.cnblogs.com/w-y-1/p/6705078.html
Copyright © 2011-2022 走看看