C++ STL學習 queue

來源:互聯網
上載者:User

標籤:遍曆   htm   判斷   容器   while   color   ios   學習   http   

本文修改自http://www.cnblogs.com/hdk1993/p/5809180.html

 

1、使用queue需要聲明標頭檔#include <queue>

2、queue 模板類需要兩個模板參數,一個是元素類型,一個容器類型,元素類型是必要的,容器類型是可選的,預設為deque 類型。

3、queue 的基本操作有:
入隊,如例:que.push(x); 將x 接到隊列的末端。
出隊,如例:que.pop(); 彈出隊列的第一個元素,注意,並不會返回被彈出元素的值。
訪問隊首元素,如例:que.front(),即最早被壓入隊列的元素。
訪問隊尾元素,如例:que.back(),即最後被壓入隊列的元素。
判斷隊列空,如例:que.empty(),當隊列空時,返回true。
訪問隊列中的元素個數,如例:que.size()

代碼栗子:

#include <iostream>#include <queue>using namespace std;int main(){    queue <int> que;    for(int i=0;i<10;i++){        que.push(i);    }    if(!que.empty()){        cout<<"隊列不為空白。"<<endl;    }    cout<<"隊尾的元素為:";    cout<<que.back()<<endl;    cout<<"遍曆隊列:"<<endl;    while(que.size()){        cout <<que.front()<<endl;        que.pop();    }    if(que.empty()){        cout<<"元素全被彈出,隊列為空白。"<<endl;    }    return 0;}

Output:

隊列不為空白。
隊尾的元素為:9
遍曆隊列:
0
1
2
3
4
5
6
7
8
9
元素全被彈出,隊列為空白。

 

C++ STL學習 queue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.