zoukankan      html  css  js  c++  java
  • queue

    转自:http://www.169it.com/article/2718050585107790752.html

     C++队列queue模板类的定义在<queue>头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型。

        C++队列Queue是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。

    C++队列Queue类成员函数如下:

    back()返回最后一个元素

    empty()如果队列空则返回真

    front()返回第一个元素

    pop()删除第一个元素

    push()在末尾加入一个元素

    size()返回队列中元素的个数

     

    queue 的基本操作举例如下:

    queue入队,如例:q.push(x); 将x 接到队列的末端。

    queue出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

    访问queue队首元素,如例:q.front(),即最早被压入队列的元素。

    访问queue队尾元素,如例:q.back(),即最后被压入队列的元素。

    判断queue队列空,如例:q.empty(),当队列空时,返回true。

    访问队列中的元素个数,如例:q.size()

     

    C++ stl队列queue示例代码1:

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
    #include <cstdlib>
     
    #include <iostream>
     
    #include <queue>
     
    using namespace std;
     
    int main()
     
    {
     
    int e,n,m;
     
    queue<int> q1;
     
    for(int i=0;i<10;i++)
     
    q1.push(i);
     
    if(!q1.empty())
     
    cout<<"dui lie bu kong ";
     
    n=q1.size();
     
    cout<<n<<endl;
     
    m=q1.back();
     
    cout<<m<<endl;
     
    for(int j=0;j<n;j++)
     
    {
     
    e=q1.front();
     
    cout<<e<<" ";
     
    q1.pop();
     
    }
     
    cout<<endl;
     
    if(q1.empty())
     
    cout<<"dui lie bu kong ";
     
    system("PAUSE");
     
    return 0;
     
    }
     

    C++ stl队列queue示例代码2:

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
    #include <iostream>
     
    #include <queue>
     
    #include <assert.h>
     
    /*
     
    调用的时候要有头文件: #include<stdlib.h> 或 #include<cstdlib> +
     
    #include<queue> #include<queue>
     
    详细用法:
     
    定义一个queue的变量 queue<Type> M
     
    查看是否为空范例 M.empty() 是的话返回1,不是返回0;
     
    从已有元素后面增加元素 M.push()
     
    输出现有元素的个数 M.size()
     
    显示第一个元素 M.front()
     
    显示最后一个元素 M.back()
     
    清除第一个元素 M.pop()
     
    */
     
    using namespace std;
     
    int _tmain(int argc, _TCHAR* argv[])
     
    {
     
    queue <int> myQ;
     
    cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl;
     
    for(int i =0; i<10 ; i++)
     
    {
     
    myQ.push(i);
     
    }
     
    for(int i=0; i<myQ.size(); i++)
     
    {
     
    printf("myQ.size():%d ",myQ.size());
     
    cout << myQ.front()<<endl;
     
    myQ.pop();
     
    }
     
    system("PAUSE");
     
    return 0;
     
    }
     
     
     
    现在 queue 是否 empty? 1
     
    myQ.size():10
     
    0
     
    myQ.size():9
     
    1
     
    myQ.size():8
     
    2
     
    myQ.size():7
     
    3
     
    myQ.size():6
     
    4
     
    请按任意键继续. . .
  • 相关阅读:
    强连通分量(Kosaraju)
    拓扑排序
    树状数组BIT
    差分
    RMQ(ST表)
    LCA(Tarjan)
    LCA(ST倍增)
    海亮SC2019 树上数数(转载)
    海亮SC
    【十二省联考2019】异或粽子/可持久化01trie
  • 原文地址:https://www.cnblogs.com/voldemorte/p/7595986.html
Copyright © 2011-2022 走看看