priority_queue
错误示范
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<math.h>
#include<vector>
#include <fstream >
#include <ctime>
#include<string>
#include<queue>
using namespace std;
class Person
{
public:
char* name;
int id;
int age;
Person(const char* name, int id, int age)
{
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->id = id;
this->age = age;
}
Person(const Person& person)
{
name= new char[strlen(person.name) + 1];
strcpy(this->name, person.name);
age=person.age;
id = person.id;
}
Person& operator=(const Person& person)
{
name = new char[strlen(person.name) + 1];
strcpy(this->name, person.name);
age = person.age;
id = person.id;
}
bool operator>(const Person& person)
{
if (this->id > person.id)
{
return false ;
}
return true;
}
bool operator>=(const Person& person)
{
if (this->id >= person.id)
{
return false;
}
return true;
}
~Person()
{
if (this->name != NULL)
{
delete[]this->name;
this->name = NULL;
}
}
};
int main()
{
Person *student1 = new Person("A", 2, 23);
Person *student2 = new Person("B", 3, 21);
Person *student3 = new Person("C", 1, 22);
if (*student1 > *student2)
{
cout << "1" << endl;
}
priority_queue<Person*>q;
q.push(student1);
q.push(student2);
q.push(student3);
int size = q.size();
for (int i = 0;i < size;i++)
{
cout << q.top()->name << endl;
q.pop();
}
}
//只能比较三个数据有序
class Edge
{
public:
int weight;
Node* from;
Node* to;
public:
Edge(int weight, Node *from, Node *to)
{
this->weight = weight;
this->to = to;
this->from = from;
}
};
struct cmp
{
bool operator()(Edge *a, Edge *b)
{
if (a->weight == b->weight) return a->weight >= b->weight;
else return a->weight > b->weight;
}
};
void test()
{
Edge a(1,NULL,NULL);
Edge b(2, NULL, NULL);
Edge c(3, NULL, NULL);
Edge d(100, NULL, NULL);
Edge e(11, NULL, NULL);
//只有重载priority_queue<Edge*> my_queq;
priority_queue<Edge*,vector< Edge*>, cmp> my_queq;
my_queq.push(&b);
my_queq.push(&a);
my_queq.push(&c);
my_queq.push(&d);
my_queq.push(&e);
while (!my_queq.empty())
{
cout << my_queq.top()->weight << " ";
my_queq.pop();
}
cout << endl;
}
//这样写才能整体有序
但是在 kruskalMST算法却会出错
下列为错误代码
set<Edge*>* kruskalMST(Graph *graph)
{
mySet myset(graph->nodes);
auto cmp2 = [](Edge* left, Edge* right) { return left->weight > right->weight; };
priority_queue<Edge*, vector< Edge*>,cmp> mypr_que;
for (set<Edge*>::iterator it = graph->edges->begin(); it != graph->edges->end(); it++)
{
cout << (*it)->weight << " ";
Edge* temp = (*it);
mypr_que.push(temp);
}
cout<< endl;
set<Edge*>* result = new set<Edge*>;
while (!mypr_que.empty())
{
Edge* edge = mypr_que.top();
cout << "weight:"<<edge->weight << endl;
mypr_que.pop();
if (!myset.isSameSet(edge->from, edge->to))
{
result->insert(edge);
myset.unionSet(edge->from, edge->to);
}
}
return result;
}
故我建议直接使用这种用法
使用auto
auto cmp2 = [](Edge* left, Edge* right) { return left->weight > right->weight; };
priority_queue<Edge*, vector< Edge*>, decltype(cmp2)> mypr_que(cmp2);
auto介绍
auto的原理就是根据后面的值,来自己推测前面的类型是什么
遍历那会
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
for(auto i : v){
cout<<i<<" ";
}
cout<<endl;
return 0;