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;
    }
  • 相关阅读:
    Android 获取自带浏览器上网记录
    android 中的 ViewPager+ Fragment
    Git分支操作
    图形验证码的识别
    mac平台搭建安卓开发环境
    [报错集合]Uncaught ReferenceError: xxx is not defined
    Centos安装Python3
    VSCode 代码格式化 快捷键
    Mac下用Parallels Desktop安装Ubuntu
    请求头headers
  • 原文地址:https://www.cnblogs.com/youxin/p/2610963.html
Copyright © 2011-2022 走看看