STL之优先级队列priority_queue
生活随笔
收集整理的這篇文章主要介紹了
STL之优先级队列priority_queue
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘要:
priority_queue,自適應容器(即容器適配器):不能由list來組建;
最大值優先級隊列(最大值始終在對首,push進去時候)
最小值優先級隊列;
優先級隊列適配器 STL ?priority_queue
priority_queue<int, deque<int> > pg;
priority_queue<int, vector<int> > pg;
STL中實現的方法:
pg.empty();
pg.size();
pg.top(); //查看隊首的元素
pg.pop(); //從隊首刪除元素;
pg.push(item); //從隊尾加入元素
1 #include <iostream> 2 #include <queue> 3 4 using namespace std; 5 int main() 6 { 7 //最大值有限隊列, 會進行自動排序 8 priority_queue<int, vector<int> > pg; 9 priority_queue<int, deque<int> > pg2; 10 //priority_queue<int> pg3; 11 12 pg.push(10); 13 pg.push(5); 14 pg.push(-1); 15 pg.push(20); 16 17 std::cout <<"priority_queue first item: " << pg.top() << std::endl; 18 while(!pg.empty()){ 19 std::cout<<"priority_queue del item: " << pg.top() << std::endl; 20 pg.pop(); 21 } 22 //最小值有限隊列,從小到大排序 23 priority_queue<int, deque<int>, greater<int> > pg3; 24 pg3.push(10); 25 pg3.push(5); 26 pg3.push(-1); 27 pg3.push(20); 28 std::cout <<"priority_queue first item: " << pg3.top() << std::endl; 29 while(!pg3.empty()){ 30 std::cout<<"priority_queue del item: " << pg3.top() << std::endl; 31 pg3.pop(); 32 } 33 34 return 0; 35 }?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的STL之优先级队列priority_queue的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: getline函数
- 下一篇: 深挖BAT内部级别和薪资待遇