C++ 優先隊列priority_queue用法

頭文件:#include<queue>

操作:

  • top 訪問隊頭
  • empty 隊列是否為空
  • size 返回隊列元素個數
  • push 插入元素到隊尾
  • pop 彈出隊頭
  • swap 交換內容

定義:

 1 /*
 2 Type 數據類型
 3 Container 容器類型(必須是vector,deque等數組實現的容器)
 4 Functional 比較方式
 5 */
 6 priority_queue<Type, Container, Functional>
 7 
 8 //實例
 9 //升序隊列
10 priority_queue <int,vector<int>,greater<int> > q;
11 //降序隊列
12 priority_queue <int,vector<int>,less<int> >q;
13 
14 //greater和less是std實現的兩個仿函數(就是使一個類的使用看上去像一個函數。其實現就是類中實現一個operator(),這個類就有了類似函數的行為,就是一個仿函數類了)

 

Tags: