当前位置:网站首页>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()
边栏推荐
- 2021-10-20
- OSI model notes
- VPS installation virtualmin panel
- Exceptions and exception handling
- re. Sub() usage
- Experiment 7. IPv6
- Summary of Shanghai Jiaotong University postgraduate entrance examination module -- cryptography
- About the use of URL, href, SRC attributes
- AI should take code agriculture? Deepmind offers a programming version of "Alpha dog" alphacode that surpasses nearly half of programmers!
- 2021-08-09
猜你喜欢

Experiment 7. IPv6
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5](/img/68/4f92ca7cbdb90a919711b86d401302.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5

Replace() function

JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14](/img/c5/dde92f887e8e73d7db869fcddc107f.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14

Day01 preliminary packet capture
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12](/img/b1/926d9b3d7ce9c5104f3e81974eef07.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12

netstat

TCP slicing and PSH understanding

QQ group administrators
随机推荐
In 2022, financial products are not guaranteed?
2021-10-20
QQ get group link, QR code
Solaris 10 network services
Possible to restore a backup of SQL Server 2014 on SQL Server 2012?
Entitas learning [3] multi context system
How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
QQ get group settings
Simple understanding of generics
Daemon xinted and logging syslogd
03_ Armv8 instruction set introduction load and store instructions
Lvs+kept realizes four layers of load and high availability
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
[Android reverse] function interception instance (③ refresh CPU cache | ④ process interception function | ⑤ return specific results)
No response after heartbeat startup
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
OSI seven layer model & unit
Serialization oriented - pickle library, JSON Library
Ultimate bug finding method - two points