【发布时间】:2022-08-03 18:55:32
【问题描述】:
我们如何将 STL priority_queue 用于 struct ?
任何插图pushing 和 popping ,其中 struct 有多种数据类型?
说:struct thing { int a; char b;} glass[10];。
现在我如何使用'int a'将这个结构放在priority_queue上进行排序?
【问题讨论】:
标签: c++ struct priority-queue
我们如何将 STL priority_queue 用于 struct ?
任何插图pushing 和 popping ,其中 struct 有多种数据类型?
说:struct thing { int a; char b;} glass[10];。
现在我如何使用'int a'将这个结构放在priority_queue上进行排序?
【问题讨论】:
标签: c++ struct priority-queue
这是对 your original question, which you deleted 的略微修改的答案,原因不明。原始文件包含足够的信息让您弄清楚这一点,但这里是这样的:提供一个使用 int 进行比较的小于比较。
您需要做的就是提供一个仿函数,该仿函数通过严格的弱排序实现小于比较,或者为您的类提供一个小于运算符来实现相同的操作。该结构满足要求:
struct thing
{
int a;
char b;
bool operator<(const thing& rhs) const
{
return a < rhs.a;
}
};
然后
std::priority_queue<thing> q;
thing stuff = {42, 'x'};
q.push(stuff);
q.push(thing{4242, 'y'}); // C++11 only
q.emplace(424242, 'z'); // C++11 only
thing otherStuff = q.top();
q.pop();
【讨论】:
q.push(thing{42, 'x'}) 或 q.emplace(42, 'x')。如果你没有 C++11 支持,你需要给thing一个构造函数。
q.emplace(424242, 'z'); // C++11 only 行是错误的,它会一直抛出错误直到 c++20。为此,结构需要有一个显式构造函数。
为 thing 重载 < 运算符:
struct thing
{
int a;
char b;
bool operator<(const thing &o) const
{
return a < o.a;
}
};
priority_queue<thing> pq;
thing t1, t2, t3;
// ...
pq.push(t1);
pq.push(t2);
// ...
t3 = pq.top();
pq.pop();
【讨论】:
你可以这样做!
struct example{
int height;
int weight;
};
struct comp{
bool operator()(struct example a, struct example b){
//Sorting on the basis of height(Just for example)
return (a.height > b.height);
}
};
// And here comes your priority queue
priority_queue<struct example, vector<struct example>, comp> pq;
struct example your_obj;
pq.push(your_obj);
【讨论】:
结构中的优先级队列可以通过两种方式实现:
让我们采用模型和价格均为整数的结构汽车,并根据顶部的最高价格构建优先级队列。
struct Car
{
int m;
int p;
void set(int a,int b){
m =a,p = b;
}
};
对于方法 1,我们需要这样做:
bool operator < (const Car& c1,const Car& c2){
return c1.p < c2.p;
}
对于方法 2,我们需要传递这个结构:
struct Cmp{
bool operator()(const Car& c1,const Car& c2){
return c1.p < c2.p;
}
};
用于说明的完整代码:::
#include <iostream>
#include<unordered_map>
#include<string>
#include<queue>
#include<vector>
using namespace std;
struct Car
{
int m;
int p;
void set(int a,int b){
m =a,p = b;
}
};
struct Car cT[50];
int cI=0;
void printC(){
for(int i=0;i<cI;i++){
cout <<" car model: " << cT[i].m << " price: " <<cT[i].p <<endl;
}
}
bool operator < (const Car& c1,const Car& c2){
return c1.p < c2.p;
} //Method 1
struct Cmp{
bool operator()(const Car& c1,const Car& c2){
return c1.p < c2.p;
}
}; //Method 2
void pushQ(priority_queue<Car,vector<Car>,Cmp>& Q){
for(int i=0;i<cI;i++){
Q.push(cT[i]);
cout <<"Queue Push car model: " << cT[i].m << " price: " <<cT[i].p <<endl;
}
};
void printQ(priority_queue<Car,vector<Car>,Cmp> Q){
while(!Q.empty()){
Car c = Q.top();
Q.pop();
cout <<" car model: " << c.m << " price: " <<c.p <<endl;
}
}
int main() {
// Write C++ code here
// priority_queue<Car> Q; // #Method 1
priority_queue<Car,vector<Car>,Cmp> Q;// #Method 2
cT[cI++].set(4,5);
cT[cI++].set(34,4);
cT[cI++].set(43,6);
cT[cI++].set(41,15);
pushQ(Q);
printQ(Q);
// printC();
// cout << "Hello world!" <<f1;
return 0;
}
输出将是:::
car model: 41 price: 15
car model: 43 price: 6
car model: 4 price: 5
car model: 34 price: 4
【讨论】:
bool operator < (const Car& c1,const Car& c2){ return c1.p < c2.p; } 这里的优先队列顺序为 Car 2 如果 return true 将优先于 Car1 它将在优先列表的上方。 < (const Car& c1,const Car& c2) ==> 如果 c2 的真实优先级高于 c1。由于优先级队列默认实现为 Max Heap Fashion。