zoukankan      html  css  js  c++  java
  • STL --> queue单向队列

    queue单向队列

      queue 模板类的定义在<queue>头文件中。与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型。

     

    定义queue 对象的示例代码:

    queue<int> q1;
    queue<double> q2;

    queue 的基本操作有:

    q.push(x)               //  入队,将x 接到队列的末端。
    q.pop()                  //  出队,弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
    q.front()                //  访问队首元素,即最早被压入队列的元素。
    q.back()                //  访问队尾元素,即最后被压入队列的元素。
    q.empty()              //  判断队列空,当队列空时,返回true。
    q.size()                  //  访问队列中的元素个数

    使用范例:

      #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;
    }
  • 相关阅读:
    android scroll 中 scroll Bar 修改
    android 在代码中安装apk的方法
    android JSON 的应用
    android 混淆相关 proguard
    listView 相关的优化设置
    android 名称解释
    android Gallery 两侧阴影实现
    Service 详解
    使用alias 简化命令
    android 动画小结
  • 原文地址:https://www.cnblogs.com/jeakeven/p/4553007.html
Copyright © 2011-2022 走看看