• STL__queue_的应用


    转:http://hi.baidu.com/xiaotiandm/item/bda34511cf9e99098fbde41a

    调用的时候要有头文件: #include<stdlib.h> 或 #include<cstdlib> + #include<queue> 
    详细用法:
    定义一个queue的变量                       queue<Type> que 
    查看是否为空范例                              que.empty()    是的话返回1,不是返回0;
    从已有元素后面增加元素(入队)      que.push() 
    现有元素的个数                                 que.size()
    显示第一个元素                                 que.front()
    显示最后一个元素                              que.back()
    清除第一个元素 (出队)                  que.pop()

    看一个例子:

    #include <cstdlib>
    #include <iostream>
    #include <queue> 
    using namespace std;
    int main()

        queue<int> myQ;
        cout<< "现在 queue 是否 empty? "<< myQ.empty() << endl; 
        cout << "push 5, push 6, push 7" << endl;
        myQ.push(5); 
        myQ.push(6); 
        myQ.push(7); 
        cout << "现在 queue 的元素有几个? " << myQ.size() << endl; 
        cout << "现在 queue 的front为何? " << myQ.front() << endl; 
        cout << "现在 queue 的rear为何? " << myQ.back() << endl; 
        cout << "pop" << endl; myQ.pop(); 
        cout << "现在 queue 的元素有几个? " << myQ.size() << endl; 
        cout << "现在 queue 的front为何? " << myQ.front() << endl; 
        cout << "现在 queue 的rear为何? " << myQ.back() << endl; 
        system("PAUSE"); 
        return 0;
    }

    关于队列的知识;

    使用queue之前,要先利用构造函数一个队列对象,才可以进行元素的入队,出队,取队首和队尾等操作;

    (1)、queue() queue<int> q; 或者 queue<int>Q[10000];

    (2)、queue(const queue&) 复制构造函数

    例如:用一行代码利用queue对象q1,创建一个以双向链表为底层容器的queue对象q2

    queue<int,list<int>>q1;

    queue<int,list<int>>q2(q1);

    (3)、元素入队 函数为:push()例如:q.push(3),意思是将3入队 ,注意队列的大小不预设

    (4)、元素出队 函数为:pop()例如:q.pop()

    (5)、取对头元素 函数为:front()

    (6)、取队尾元素 函数为:back()

    (7)、判断对空    函数为:empty()

    (8)、队列的大小 函数为:size()返回队列的当前元素的个数

    (9)、如何实现固定大小的queue队列

    在每一次元素入队列前都判断当前的队列是否满,用双向链表做queue 的底层容器可以实现

    例如:

    #include<iostream>
    #include<list>
    #include<queue>
    using namespace std;
    #define QUEUE_SIZE 50 //固定大小;
    int main()
    {
    queue<int,list<int>> q;
    if(q.size(QUEUE_SIZE) )
       q.push(51);
    if(q.size(QUEUE_SIZE) )
       q.push(36);
    if(q.size(QUEUE_SIZE))
       q.push(28);
    while(!q.empty() )
    {
       cout<<q.front()<<endl;
       q.pop();
    }
    return 0;
    }


    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<stdlib>
    using namespace std;
    int main()
    {
    register int i,j;
    int m,n;
    char c[6];
    int x,y;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
       queue<int> Q[10000];
       for(i=1;i<m;i++)
       {
        scanf("%s",&c);
        if(strcmp(c,"INT") == 0)
        {
         for(j=1;j<=n;j++)
         {
          while(!Q[j].empty())
          {
           Q[j].pop();
          }
          continue;
         }
        }
        if(strcmp(c,"PUSH")==0)
        {
         scanf("%d %d",&x,&y);
         Q[x].push(y);
         continue;
        }
        if(strcmp(c,"POP")==0)
        {
         scanf("%d ",&y);
         if(Q[y].empty)
         {
          printf("NULL ");continue;
         }
         else
         {
          printf("%d ",Q[y].front());
         }
         Q[y].pop;
        }
       }
    }
    return 0;
    }

  • 相关阅读:
    UVALive 5966 Blade and Sword -- 搜索(中等题)
    UVA 12380 Glimmr in Distress --DFS
    【转】最长回文子串的O(n)的Manacher算法
    UVA 12382 Grid of Lamps --贪心+优先队列
    UVA 12377 Number Coding --DFS
    高斯消元模板
    图的全局最小割的Stoer-Wagner算法及例题
    逻辑运算符短路特性的应用
    为什么在 Java 中用 (low+high)>>>1 代替 (low+high)/2 或 (low+high)>>1 来计算平均值呢?好在哪里?
    数据库读写分离和数据一致性的冲突
  • 原文地址:https://www.cnblogs.com/lanye/p/3818773.html
Copyright © 2020-2023  润新知