STL priority_queue是堆数据结构的实现。默认情况下,它是最大堆,我们可以轻松地将其用于原始数据类型。它有一些重要的应用,可以在这里找到
先决条件: Prioirty_queue基础
在本文中,我们将看到如何对诸如类或结构之类的自定义数据类型使用priority_queue。
假设我们有一个结构名称Person,它由两个变量Age和height组成
并且我们想将其存储在priority_queue中,那么一个简单的方法将无法在这里工作。
以下是struct Person声明的示例:
C++
struct Person{
int Age;
float Height;
}
C++
priority_queue pq;
C++
// program in c++ to use priority_queue with structure
#include
#include
using namespace std;
#define ROW 5
#define COL 2
struct Person {
int age;
float height;
// this will used to initialize the variables
// of the structure
Person(int age, float height)
: age(age), height(height)
{
}
};
// this is an strucure which implements the
// operator overloading
struct CompareHeight {
bool operator()(Person const& p1, Person const& p2)
{
// return "true" if "p1" is ordered
// before "p2", for example:
return p1.height < p2.height;
}
};
int main()
{
priority_queue, CompareHeight> Q;
// When we use priority_queue with structure
// then we need this kind of syntax where
// CompareHeight is the functor or comparison function
float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
{ 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
for (int i = 0; i < ROW; ++i) {
Q.push(Person(arr[i][0], arr[i][1]));
// insert an object in priority_queue by using
// the Person strucure constructor
}
while (!Q.empty()) {
Person p = Q.top();
Q.pop();
cout << p.age << " " << p.height << "\n";
}
return 0;
}
C++
// program in c++ to use priority_queue with class
#include
#include
using namespace std;
#define ROW 5
#define COL 2
class Person {
public:
int age;
float height;
// this is used to initialize the variables of the class
Person(int age, float height)
: age(age), height(height)
{
}
};
// we are doing operator overloading through this
bool operator<(const Person& p1, const Person& p2)
{
// this will return true when second person
// has greater height. Suppose we have p1.height=5
// and p2.height=5.5 then the object which
// have max height will be at the top(or
// max priority)
return p1.height < p2.height;
}
int main()
{
priority_queue Q;
float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
{ 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
for (int i = 0; i < ROW; ++i) {
Q.push(Person(arr[i][0], arr[i][1]));
// insert an object in priority_queue by using
// the Person class constructor
}
while (!Q.empty()) {
Person p = Q.top();
Q.pop();
cout << p.age << " " << p.height << "\n";
}
return 0;
}
如下所示定义优先级队列时,由于priority_queue不知道我们需要按什么顺序(最小或最大)来排列对象,因此会给我们带来错误。
C++
priority_queue pq;
为了纠正上述错误,我们将使用运算符重载来定义优先级。这样,priority_queue可以决定如何存储结构对象。
下面给出的是priority_queue实现,其结构如下:
C++
// program in c++ to use priority_queue with structure
#include
#include
using namespace std;
#define ROW 5
#define COL 2
struct Person {
int age;
float height;
// this will used to initialize the variables
// of the structure
Person(int age, float height)
: age(age), height(height)
{
}
};
// this is an strucure which implements the
// operator overloading
struct CompareHeight {
bool operator()(Person const& p1, Person const& p2)
{
// return "true" if "p1" is ordered
// before "p2", for example:
return p1.height < p2.height;
}
};
int main()
{
priority_queue, CompareHeight> Q;
// When we use priority_queue with structure
// then we need this kind of syntax where
// CompareHeight is the functor or comparison function
float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
{ 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
for (int i = 0; i < ROW; ++i) {
Q.push(Person(arr[i][0], arr[i][1]));
// insert an object in priority_queue by using
// the Person strucure constructor
}
while (!Q.empty()) {
Person p = Q.top();
Q.pop();
cout << p.age << " " << p.height << "\n";
}
return 0;
}
输出 :
33 6.1
20 6
23 5.6
30 5.5
25 5
下面给出的是使用Class实现的priority_queue
C++
// program in c++ to use priority_queue with class
#include
#include
using namespace std;
#define ROW 5
#define COL 2
class Person {
public:
int age;
float height;
// this is used to initialize the variables of the class
Person(int age, float height)
: age(age), height(height)
{
}
};
// we are doing operator overloading through this
bool operator<(const Person& p1, const Person& p2)
{
// this will return true when second person
// has greater height. Suppose we have p1.height=5
// and p2.height=5.5 then the object which
// have max height will be at the top(or
// max priority)
return p1.height < p2.height;
}
int main()
{
priority_queue Q;
float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
{ 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
for (int i = 0; i < ROW; ++i) {
Q.push(Person(arr[i][0], arr[i][1]));
// insert an object in priority_queue by using
// the Person class constructor
}
while (!Q.empty()) {
Person p = Q.top();
Q.pop();
cout << p.age << " " << p.height << "\n";
}
return 0;
}
输出 :
33 6.1
20 6
23 5.6
30 5.5
25 5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。