一、queue簡介
queue所有元素的進出都必須符合”先進先出”的條件,只有queue的頂端元素,才有機會被外界取用,queue不提供遍歷功能,也不提供迭代器,
queue是簡單地裝飾deque容器而成為另外的一種容器,
#include <queue>

二、queue物件的默認構造
queue采用模板類實作,queue物件的默認構造形式:queue<T> queT;
queue<int> queInt; //一個存放int的queue容器,
queue<float> queFloat; //一個存放float的queue容器,
queue<string> queString; //一個存放string的queue容器,
//尖括號內還可以設定指標型別或自定義型別,
三、queue的push()與pop()方法
queue.push(elem); //往隊尾添加元素
queue.pop(); //從隊頭移除第一個元素
queue<int> queInt;
queInt.push(1);
queInt.push(3);
queInt.push(5);
queInt.push(7);
queInt.push(9);
queInt.pop();
queInt.pop();
//此時queInt存放的元素是5,7,9
四、queue物件的拷貝構造與賦值
queue(const queue &que); //拷貝建構式
queue& operator=(const queue &que); //多載等號運算子
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queue<int> queIntB(queIntA); //拷貝構造
queue<int> queIntC;
queIntC = queIntA; //賦值
五、queue的資料存取
queue.back(); //回傳最后一個元素
queue.front(); //回傳第一個元素
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
int iFront = queIntA.front(); //1
int iBack = queIntA.back(); //9
六、queue的大小
queue.empty(); //判斷佇列是否為空
queue.size(); //回傳佇列的大小
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
if (!queIntA.empty()){
int iSize = queIntA.size(); //5
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/252423.html
標籤:C++
上一篇:STL_stack容器
下一篇:STL_list容器
