當(dāng)前位置:
首頁 >
STL之优先级队列priority_queue
發(fā)布時(shí)間:2025/4/16
49
豆豆
生活随笔
收集整理的這篇文章主要介紹了
STL之优先级队列priority_queue
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
摘要:
priority_queue,自適應(yīng)容器(即容器適配器):不能由list來組建;
最大值優(yōu)先級隊(duì)列(最大值始終在對首,push進(jìn)去時(shí)候)
最小值優(yōu)先級隊(duì)列;
優(yōu)先級隊(duì)列適配器 STL ?priority_queue
priority_queue<int, deque<int> > pg;
priority_queue<int, vector<int> > pg;
STL中實(shí)現(xiàn)的方法:
pg.empty();
pg.size();
pg.top(); //查看隊(duì)首的元素
pg.pop(); //從隊(duì)首刪除元素;
pg.push(item); //從隊(duì)尾加入元素
1 #include <iostream> 2 #include <queue> 3 4 using namespace std; 5 int main() 6 { 7 //最大值有限隊(duì)列, 會(huì)進(jìn)行自動(dòng)排序 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 //最小值有限隊(duì)列,從小到大排序 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 }?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的STL之优先级队列priority_queue的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: getline函数
- 下一篇: 深挖BAT内部级别和薪资待遇