C++ STL queue

来源:互联网 发布:淘宝店铺类目是什么 编辑:程序博客网 时间:2024/04/29 22:54
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
0 0
原创粉丝点击