zoukankan      html  css  js  c++  java
  • C++ STL queue用法

    FIFO queue

    queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

    queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".

    The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:

    • front()
    • back()
    • push_back()
    • pop_front()


    Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.

    In their implementation in the C++ Standard Template Library, queues take two template parameters:

         template < class T, class Container = deque<T> > class queue;
    • push(x)  将元素压入队列
    • pop()   弹出首部元素
    • front()   获取首部元素
    • back()  获取尾部元素
    • empty()  队列为空则返回1,不为空返回0
    • size() 返回队列中元素的个数
    // queue::push/pop
    #include <iostream>
    #include <queue>
    using namespace std;
    
    int main ()
    {
      queue<int> myqueue;
      int myint;
    
      cout << "Please enter some integers (enter 0 to end):\n";
    
      do {
        cin >> myint;
        myqueue.push (myint);
      } while (myint);
    
      cout << "myqueue contains: ";
      while (!myqueue.empty())
      {
        cout << " " << myqueue.front();
        myqueue.pop();
      }
    
      return 0;
    }
  • 相关阅读:
    替换URL传递的参数
    执行SQl语句得到xml结果集
    table中文本太长换行
    org.xml.sax.SAXNotRecognizedException
    WAMP+CMSeasy快速搭建学校网站
    推荐几个web前台开发的小工具
    来园子里注册啦
    C++ Virtual的背后
    Games101观后补充笔记
    Lua语法入门
  • 原文地址:https://www.cnblogs.com/youxin/p/2610963.html
Copyright © 2011-2022 走看看