priority_queue重写比较器

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;

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值