当前位置:网站首页>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()
边栏推荐
- Common tips
- Experiment 7. IPv6
- Cacti主机模板之定制版
- LxC shared directory permission configuration
- Entitas learning [3] multi context system
- 'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
- How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
- Exceptions and exception handling
- OSI model notes
- Heartbeat error attempted replay attack
猜你喜欢

Simple understanding of seesion, cookies, tokens

Notes on writing test points in mind mapping

Decrypt the advantages of low code and unlock efficient application development

Reptile learning 4 winter vacation series (3)

(August 10, 2021) web crawler learning - Chinese University ranking directed crawler

Clion configuration of opencv

Properties and methods of OS Library

Practical dry goods: deploy mini version message queue based on redis6.0
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24](/img/2e/b1f348ee6abaef24b439944acf36d8.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
![Entitas learning [3] multi context system](/img/f9/a3ce86ff2121dd1043305b7e834cc5.jpg)
Entitas learning [3] multi context system
随机推荐
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
Reptile learning 4 winter vacation learning series (1)
Introduction to Lichuang EDA
Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
QQ set group information
Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography
Local MySQL forgot the password modification method (Windows)
Usage of with as
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
QQ group administrators
Fundamentals of software testing
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
A few words explain redis cache penetration, breakdown, avalanche, and redis sentinel
TCP slicing and PSH understanding
JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
If function in SQL
DVC use case (VI): Data Registry
(August 9, 2021) example exercise of air quality index calculation (I)
Iframe to only show a certain part of the page