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;
    }
  • 相关阅读:
    推荐一款超棒的阅读App
    IntelliJ中的main函数和System.out.println()快捷键
    oracle中varchar2字段存入blob字段及blob转成varchar2
    闭包
    some of the properties associated with the solution could not be read解决方法
    Visual Studio 2010如何利用宏
    中高级程序员成长必备素质
    WORD小技巧
    de4dot 用法
    JavaScript学习记录
  • 原文地址:https://www.cnblogs.com/youxin/p/2610963.html
Copyright © 2011-2022 走看看