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;
    }
  • 相关阅读:
    ADC推荐:测量Flash的视频消费行为 (转载)
    9.7Go之函数之递归函数
    9.8线性表之单链表
    9.7线性表之顺序表
    9.7顺序表之增、删、改、查
    9.8Go之函数之计算执行时间
    9.8Go之函数之宕机(panic)
    9.9Go语言内存缓存
    9.7Go之函数之处理RuntimeError
    9.7Go之函数之defer(延迟执行语句)
  • 原文地址:https://www.cnblogs.com/youxin/p/2610963.html
Copyright © 2011-2022 走看看