本文主要是介绍【C++ STL】细数C++ STL 的那些事 -- queue(队列),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一,概述
先进先出的数据结构,底端加入元素,顶端移除元素,类似stack同样不能有遍历行为,没有迭代器。也是以既有容器为底端容器被归类为陪接器(container adapter),默认底端容器为deque。
二,使用
#include <queue>
using namespace std;
三,方法
queue::push( ); //底部插入元素
queue::pop( ); //顶端移除元素
queue::empty( ); //是否为空
queue::back( );
queue::front( );
queue::size( );
四,示例
#include <list>
#include <iostream>
#include <queue>
#include <deque>
using namespace std ;// Using queue with list
typedef list<int > INTLIST;
typedef queue<int,INTLIST> INTQUEUE;// Using queue with deque
typedef deque<const char*> CHARDEQUE;
typedef queue<const char*,CHARDEQUE> CHARQUEUE;int main(void)
{size_t size_q;INTQUEUE q;CHARQUEUE p;// Insert items in the queue(uses list)q.push(42);q.push(100);q.push(49);q.push(201);size_q = q.size();cout << "size of q is:" << size_q << endl;while (!q.empty()){cout << q.front() << " ";q.pop();}p.push("cat");p.push("ape");p.push("dog");p.push("mouse");p.push("horse");cout << "\n"<<"p.back:"<<p.back() << endl;//the last elemetsize_q = p.size();cout << "size of p is:" << size_q << endl;while (!p.empty()){cout << p.front() <<" ";p.pop();}
}
这篇关于【C++ STL】细数C++ STL 的那些事 -- queue(队列)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!