当前位置:网站首页>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()
边栏推荐
- Introduction of network security research direction of Shanghai Jiaotong University
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
- Local MySQL forgot the password modification method (Windows)
- How to use the mongodb ID array to get multiple documents- How to get multiple document using array of MongoDb id?
- thread
- Lecture 9
- QQ get group information
- Detailed array expansion analysis --- take you step by step analysis
- Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
- netstat
猜你喜欢
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22](/img/e0/21367eeaeca10c0a2f2aab3a4fa1fb.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22

Usage of case when then else end statement

netstat

Serialization oriented - pickle library, JSON Library
![Entitas learning [3] multi context system](/img/f9/a3ce86ff2121dd1043305b7e834cc5.jpg)
Entitas learning [3] multi context system
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5](/img/68/4f92ca7cbdb90a919711b86d401302.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8](/img/16/33f5623625ba817e6e022b5cb7ff5d.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8

(August 9, 2021) example exercise of air quality index calculation (I)
![[solve the error of this pointing in the applet] SetData of undefined](/img/19/c34008fbbe1175baac2ab69eb26e05.jpg)
[solve the error of this pointing in the applet] SetData of undefined

Detailed explanation of classic process synchronization problems
随机推荐
QQ group administrators
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
Understanding of object
Enter the smart Park, and change begins here
Recommend a cool geospatial data visualization tool with low code
2021 annual summary - it seems that I have done everything except studying hard
2021-10-20
Video analysis
[solve the error of this pointing in the applet] SetData of undefined
array_ The contains() function uses
守护进程Xinted和日志记录Syslogd
Common tips
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Introduction to Lichuang EDA
OSI seven layer reference model
(August 9, 2021) example exercise of air quality index calculation (I)
Notes on writing test points in mind mapping
Some summaries of the 21st postgraduate entrance examination 823 of network security major of Shanghai Jiaotong University and ideas on how to prepare for the 22nd postgraduate entrance examination pr
Realize cross tenant Vnet connection through azure virtual Wan
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20