zoukankan      html  css  js  c++  java
  • STL之Queue(Q)

    STL的Queue(数据结构中的队列):

      特点:FIFO 先进先出;

         自适应容器(即容器适配器)

       栈适配器STL queue

       STL中实现的Queue:

        用list来实现queue; queue<int, list<int> >      q;

        用deque来实现queue; queue<int, deque<int> >   q;

        不能用vector来实现queue;

      STL中Queue实现的方法(6种):

        q.empty();

        q.size();

        q.front();

        q.back();

        q.pop();

        q.push(item);

      STL的queue没有迭代器,它只能操作队列头、队列尾的元素,而不能操作队列中间的元素;一般情况下,queue用于系统软件开发,编译器开发;

     1 #include <list>
     2 #include <deque>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     queue<int, deque<int> > a;
     9     queue<int, list<int> >  b;  
    10     //queue<int, vector<int> > c; //error
    11     queue<int> q;  //相当于queue<int, deque<int> > q;
    12     
    13     q.push(10); //队列只能从队尾插入
    14     q.push(5);
    15     q.push(-1);
    16     q.push(20);
    17     
    18     std::cout<<"queue size is "<<q.size()<<std::endl;
    19     std::cout << "queue first item:" << q.front() << std::endl; //q.front() 查看队列头元素
    20     std::cout <<"queue end item: " <<q.back() <<std::endl;      //q.front() 查看队列尾元素
    21 
    22     q.pop();    //队列只能从头删除
    23     std::cout << "queue new first item:" << q.front() << std::endl;
    24         
    25     while(q.size() !=0)
    26     {   
    27         std::cout << "del item :" <<q.front() << std::endl;
    28         q.pop();
    29     }
    30 
    31     if(q.empty())
    32     {
    33         std::cout <<"now queue is empty "<<std::endl;
    34     }
    35 
    36     return 0;
    37 }
  • 相关阅读:
    Visual Studio LightSwitch
    Android 虚拟机与真机调试配置
    点击手机 menu 硬件按钮后的显示及处理
    Windows Phone 7 真机调试
    Android 调试
    Android 新建项目 页面
    今天我的Windows Phone 7 HTC HD7 手机 升级 NoDo 了 分享一下经验
    Activity 之间调用与参数传递
    Android widget 组件
    解决 warning: found plain 'id' attribute; did you mean the new 'android:id' name? 问题
  • 原文地址:https://www.cnblogs.com/chris-cp/p/4510556.html
Copyright © 2011-2022 走看看