priority_queuey用法
priority_queue優先隊列,插入進去的元素都會從大到小排好序
PS:在priority_queue<ll, vector<ll>, greater<ll> > pq;中
第一個參數為數據類型,第二個參數為保存數據的容器(默認為vector<int>),第三個參數為元素比較函數(默認為less)。//默認算子為less,即小的往前排,大的往后排(出隊時序列尾的元素出隊)
優先隊列試圖將兩個元素x和y代入比較運算符(對less算子,調用x<y,對greater算子,調用x>y),若結果為真,則x排在y前面,y將先于x出隊,反之,則將y排在x前面,x將先出隊。
STL里面默認用的是 vector. 比較方式默認用 operator< , 所以如果你把后面倆個參數缺省的話,優先隊列就是大頂堆,隊頭元素最大。
基本操作:
empty() 如果隊列為空返回真
pop() 刪除隊頂元素
push() 加入一個元素
size() 返回優先隊列中擁有的元素個數
top() 返回優先隊列隊頂元素(隊列中的front()變成了top())
在默認的優先隊列中,優先級高的先出隊。在默認的int型中先出隊的為較大的數。
例子
UVA136 - Ugly Numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500’th ugly number.
Input
There is no input to this program.
Output
Output should consist of a single line as shown below, with ‘’ replaced by the number
computed.
Sample Output
The 1500’th ugly number is <number>
總結
以上是生活随笔為你收集整理的priority_queuey用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css 回到首页按钮,js+css实现回
- 下一篇: 计算机科学陈国华,科学网—模式识别与智能