现在的位置: 首页 > 综合 > 正文

C++ STL queue

2019年06月07日 ⁄ 综合 ⁄ 共 1285字 ⁄ 字号 评论关闭
template <class T, class Container = deque<T> > class 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. This underlying container shall support at least the following operations:

  • front
  • back
  • push_back
  • pop_front

The standard container classes deque and list fulfill
these requirements. By default, if no container class is specified for a particular queue class instantiation, the standard container deque is
used.

#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main(){
queue<string> q;
q.push("These ");
q.push("are ");
q.push("more than ");

cout<<q.front();
q.pop();
cout<<q.front();
q.pop();

q.push("four ");
q.push("words!");
q.pop();

cout<<q.front();
q.pop();
cout<<q.front()<<endl;
q.pop();

cout<<"number of elements in the queue:"<<q.size()<<endl;

return 1;
}

编译后输出:
These are four words!
number of elements in the queue:0
【上篇】
【下篇】

抱歉!评论已关闭.