1. 程式人生 > 其它 >c++中queue標頭檔案的使用

c++中queue標頭檔案的使用

技術標籤:資料結構佇列資料結構

標頭檔案

#include < queue >

定義

queue< int > a;
queue< string > s;

常用函式:

1 push() :在佇列尾部插入元素
2 pop (): 移除最頂端的資料
3 size (): 輸出佇列中資料元素的個數
4 empty() : 判斷佇列是否為空
5 front ():返回佇列中第一個元素,但是並不刪除
6 back ():返回佇列中最後一個元素,並且不刪除

簡單應用程式碼:

#include <iostream>
#include <queue>
using namespace std; int main(){ queue<int> s; s.push(6); s.push(3); s.push(1); s.push(7); s.push(5); s.push(8); s.push(9); s.push(2); s.push(4); while(s.size()){ cout<<s.front(); s.pop(); s.push(s.front()); s.pop(); } // int a[101]={0,6,3,1,7,5,8,9,2,4}; // int head=1; // int tail=10;
// while(head<tail){ // cout<<a[head]<<" "; // head++; // // a[tail]=a[head]; // tail++; // head++; // // } return 0; }

結果:

615947283