C++ STL - queue使用詳解


   c++隊列模板類的定義在<queue>頭文件中,queue 模板類需要兩個模板參數,一個是元素類型,一個容器類型,元素類型是必要的,容器類型是可選的,默認為deque 類型。

下面詳細介紹queue的使用:

一:定義queue(要有頭文件#include <queue>)

queue<int> q1;
queue<double> q2;

二:基本函數

back()返回一個引用,指向隊列的最后一個元素。

empty()函數返回真(true)如果隊列為空,否則返回假(false)。

front()返回隊列第一個元素的引用。

pop()函數刪除隊列的一個元素

push() 在末尾加入一個元素

size() 返回隊列中元素的個數

三:示例代碼

#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
int main()
{
    int e,n,m;
    queue<int> q1;
    for(int i=0;i<10;i++)
       q1.push(i);
    if(!q1.empty())
    cout<<"dui lie  bu kongn";
    n=q1.size();
    cout<<n<<endl;
    m=q1.back();
    cout<<m<<endl;
    for(int j=0;j<n;j++)
    {
       e=q1.front();
       cout<<e<<" ";
       q1.pop();
    }
    cout<<endl;
    if(q1.empty())
    cout<<"dui lie  bu kongn";
    system("PAUSE");
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM