当前位置:网站首页>priority_ Understanding of queue
priority_ Understanding of queue
2022-06-29 02:48:00 【Madness makes freedom】
priority_queue The higher the default value, the higher the priority , By default, sort by less than , But you become bigger than , That is to change the less than sort to greater than sort , The lower the value, the higher the priority .
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string>
using namespace std;
//struct fruit
//{
// string name;
// int price;
// friend bool operator < (fruit f1,fruit f2) // Nonmember functions need to be defined as friend functions
// {
// return f1.price>f2.price;
// }
//}f1,f2,f3;
/**
Below operator< Is a member function of a class , His parameter is one less than the actual operand ,
Because there is a this The pointer , He points to the left operand , You can use this this The pointer
To call this operand
*/
struct fruit
{
string name;
int price;
operator < (fruit f1) const
{
return price>f1.price;
}
}f1,f2,f3;
/**
You can define the structure ( class ) The operator is overloaded when , Of course, when the overloaded operator is a member function , Member functions
Of ( Show ) The number of parameters is one less than the number of operands ,this Bind to left operand ;
When it is not a member function , To define as a friend function , That is, add the keyword before the function friend , At this point, the parameters of the friend function
And the number of general functions is the same
*/
int main()
{
priority_queue<fruit> q;
f1.name="taozi";
f1.price=3;
f2.name="lizi";
f2.price=4;
f3.name="pingguo";
f3.price=1;
q.push(f1);
q.push(f2);
q.push(f3);
cout << q.top().name <<' ' << q.top().price << endl;
printf("Hello world!\n");
return 0;
}
边栏推荐
- Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
- PWN beginner level0
- 50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
- 高并发的理解与设计方案
- Sysbench Pressure Test Oracle (installation and use examples)
- 如何在关闭socket连接的时候跳过TIME_WAIT的等待状态
- 1110: 最近共同祖先(函数专题)
- 对补wasm环境的一些测试
- String segment combination
- Download and installation of MySQL
猜你喜欢
随机推荐
【无标题】
对补wasm环境的一些测试
解决allegro中测量距离时,点击一个点后光标闪烁的问题
matlab习题 —— 图像绘制练习
String length
Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter
Relationship between EMC, EMI and EMS
Kubernetes: container resource requirements and constraints (constraints)
How does sound amplify weak sounds
Download and installation of MySQL
18. `bs object Node name next_ Sibling` get sibling nodes
There's a mystery behind the little login
Trigonometric function calculation
Relations EMC, EMI, EMS
LabVIEW generate application (exe) and installer
Informatics Olympiad all in one 1361: production | Luogu P1037 [noip2002 popularization group] production
PHP database ODBC
Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)
Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
arraylist基操和添加元素源码








