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 ascontainers adaptors, which are classes that use an encapsulated object of a specific container class as itsunderlying container, providing a specific set of member functions to access its elements. Elements arepushedinto the"back"of the specific container andpoppedfrom 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 templatesdequeandlistcan be used. By default, if no container class is specified for a particularqueueclass, the standard container class templatedequeis used.



In their implementation in the C++ Standard Template Library, queues take two template parameters:
template<classT,classContainer = deque >classqueue;
* 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;
}

原文链接: https://www.cnblogs.com/youxin/archive/2012/07/26/2610963.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/56828

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午7:39
下一篇 2023年2月9日 上午7:39

相关推荐