当前位置:网站首页>priority_ queue
priority_ queue
2022-07-04 11:55:00 【Rivers overflow】
#include<queue>
using std::priority_queue;
1. Definition
A little
2. Element access
Only use top(), Go back to team leader ( It can also be said that the top element ), That is, the element with the highest priority
3. Common function parsing
(1)push()
Time complexity O(logn),n Is the number of elements in the current queue
(2)top()
O(1)
(3)pop()
The first element out of the team ,O(logn)
(4)empty()
true-> empty ,false-> Non empty
(5)size()
4. Priority settings
① Basic data type
priority_queue<typename>;
priority_queue<typename,vector<typename>,less<typename> >;
priority_queue<typename,vector<typename>,greater<typename> >;
1. The first parameter is zero priority_queue Type of internal element
2. The second parameter is the underlying data structure ( Pile up ) The container of , The general default is vector<typename>, change typename that will do
3. The third parameter is less<typename>( The higher the number, the higher the priority ),greater<typename>( Small numbers have high priority ), That is, the first element of the team is less Is the maximum , stay greater It's the smallest , The default is less( and sort Of cmp contrary )
② Type of structure
1. Overloaded operators inside the structure
struct student
{
int index;
int score;
friend bool operator < (student s1,student s2)
{
return s1.score<s2.score;
}
};
// Call directly with
priority_queue<student> p1;
Be careful :
The effect here is similar to sort On the contrary , stay sort This definition will minimize the team leader , But because of priority_queue The default rule is to put the higher priority first , Pay attention to distinguish between
Besides, it can only be overloaded < Symbol , Otherwise it will compile incorrectly , And don't worry about it > And ==, They can be based on < Relationship to transform
2. Overloaded functions outside the structure
struct cmp
{
bool operator () (student s1,student s2)
{
return s1.score<s2.score;
}
};
// When calling, change the third parameter to the defined structure cmp
priority_queue<student,vector<student>,cmp> p;
Be careful : If the data in the structure is too large , If a string or array appears , It is recommended to use references to improve efficiency , That is to add const and &
/* Within the structure is defined */
struct student
{
int score;
int index;
friend bool operator < (const student &s1,const student &s2)
{
return s1.score<s2.score;
}
};
/* Definition of structure outside */
struct cmp
{
bool operator () (const student &s1,const student &s2)
{
return s1.score<s2.score;
}
};
Be careful : Use top() Pre attention empty()
边栏推荐
- (August 9, 2021) example exercise of air quality index calculation (I)
- DDS-YYDS
- How to use the mongodb ID array to get multiple documents- How to get multiple document using array of MongoDb id?
- re. Sub() usage
- OSI seven layer model & unit
- 'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
- Simple understanding of string
- Install freeradius3 in the latest version of openwrt
- Experiment 7. IPv6
- How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
猜你喜欢
Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
Alibaba cloud server connection intranet operation
2020 Summary - Magic year, magic me
(2021-08-20) web crawler learning 2
How to create a new virtual machine
SQL greatest() function instance detailed example
Introduction to Lichuang EDA
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Reptile learning 4 winter vacation series (3)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
随机推荐
Polymorphic system summary
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
LVS+Keepalived实现四层负载及高可用
Global function Encyclopedia
Some tips on learning database
Workplace liquor bureau must pay attention to
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
AI should take code agriculture? Deepmind offers a programming version of "Alpha dog" alphacode that surpasses nearly half of programmers!
QQ get group link, QR code
Xiaobing · beauty appraisal
QQ one click cookie acquisition
Four sorts: bubble, select, insert, count
2020 Summary - Magic year, magic me
OSI model notes
[solve the error of this pointing in the applet] SetData of undefined
Getting started with window functions
IO stream ----- open
Climb Phoenix Mountain on December 19, 2021
Realize cross tenant Vnet connection through azure virtual Wan
Understanding of object