用 STL priority queue 和 自定义比较函数 实现优先队列

#include "common.h"
#include<queue>
#include<vector>
using namespace std;

#define nullptr 0
#define u64 unsigned long long int
#define u32 unsigned int

typedef struct LinkNode
{
	u32 idx;
	u32 price;
	//在结构体里定义,只能 push 结构体对象, 如果存在结构体指针,则排序失败
	bool operator < (const LinkNode &b) const //最后的const 不能缺失,不然会报错
	{
		if ((*this).price > b.price  //价格越大,优先级越高, 价格一样, id 越小, 优先级越高 , 这里的 this 指针相当于 下面那种模式下的 a 对象指针
			|| ((*this).price == b.price && (*this).idx < b.idx))
		{
			return false; //大根堆, 优先级最高在队头
		}
		return true;
	}
}LinkNode;

#define maxmemsize 1007
static LinkNode g_n_mem[maxmemsize];
static int g_n_mem_cnt = 0;

struct compare
{
	bool operator()(LinkNode *a, LinkNode *b) //operator 不能换成其他, 比如 compare
	{
		if (a->price > b->price //价格越大,优先级越高, 价格一样, id 越小, 优先级越高
			|| (a->price == b->price && a->idx < b->idx))
		{
			return false; //优先级最高在队头
		}
		return true;
	}
};

//函数对象类
template <typename T>
class compare_ptr
{
public: //这里必须设置 公有 成员类型
	bool operator()(LinkNode *a, LinkNode *b) //operator 不能换成其他, 比如 compare
	{
		if (a->price > b->price //价格越大,优先级越高, 价格一样, id 越小, 优先级越高
			|| (a->price == b->price && a->idx < b->idx))
		{
			return false; //优先级最高在队头
		}
		return true;
	}
};

//三种定义方法,其中前两种模式最好, 只需要保存指针, 可定制对应的比较函数, 
priority_queue<LinkNode*, vector<LinkNode*>, compare> mStructQ;
priority_queue<LinkNode*, vector<LinkNode*>, compare_ptr<LinkNode *>> mClassQ;  //如果没有template <typename T> , 则定义同上也是可以的
priority_queue<LinkNode> mObjectQ; //这里不能设置 LinkNode *, 否则 push 后排序无效

void test_STL_priority_queue()
{
	for (int i = 0; i < 10; i++)
	{
		LinkNode *pNode = &g_n_mem[g_n_mem_cnt++];
		pNode->idx = i;
		pNode->price = 1 + rand() % (20 - 10 + 1) + 10;
		mStructQ.push(pNode);
	}

	LOGE("mStructQ.size() = %d", mStructQ.size());
	while (!mStructQ.empty())
	{
		LinkNode *pNode = mStructQ.top();
		LOGE("mStructQ display : %d %d", pNode->price, pNode->idx);
		mStructQ.pop();
	}

	LOGE("\n");

	for (int i = 0; i < 10; i++)
	{
		LinkNode *pNode = &g_n_mem[g_n_mem_cnt++];
		pNode->idx = i;
		pNode->price = 1 + rand() % (20 - 10 + 1) + 10;
		mClassQ.push(pNode);
	}

	LOGE("mClassQ.size() = %d", mClassQ.size());
	while (!mClassQ.empty())
	{
		LinkNode *pNode = mClassQ.top();
		LOGE("mClassQ display : %d %d", pNode->price, pNode->idx);
		mClassQ.pop();
	}

	LOGE("\n");

	for (int i = 0; i < 10; i++)
	{
		LinkNode pNode = g_n_mem[g_n_mem_cnt++];
		pNode.idx = i;
		pNode.price = 1 + rand() % (30 - 10 + 1) + 10;
		LOGE("push %d %d", pNode.price, pNode.idx);
		mObjectQ.push(pNode);
	}

	LOGE("mObjectQ.size() = %d", mObjectQ.size());
	while (!mObjectQ.empty())
	{
		LinkNode pNode = mObjectQ.top();
		LOGE("mObjectQ display : %d %d", pNode.price, pNode.idx);
		mObjectQ.pop();
	}
}

output :

e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 75 : mStructQ.size() = 10
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 21 7
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 20 1
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 20 2
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 19 0
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 18 4
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 16 5
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 16 6
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 12 3
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 12 8
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 79 : mStructQ display : 11 9
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 83 :

e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 93 : mClassQ.size() = 10
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 20 8
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 19 3
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 18 0
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 18 1
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 18 5
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 18 7
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 17 4
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 16 2
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 14 6
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 97 : mClassQ display : 13 9
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 101 :

e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 20 0
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 20 1
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 28 2
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 17 3
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 30 4
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 24 5
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 23 6
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 16 7
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 31 8
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 108 : push 19 9
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 112 : mObjectQ.size() = 10
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 31 8
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 30 4
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 28 2
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 24 5
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 23 6
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 20 0
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 20 1
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 19 9
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 17 3
e:\c++\c_test\c_test\stl.cpp test_STL_priority_queue 116 : mObjectQ display : 16 7


e:\c++\c_test\c_test\c_test.cpp main 44 : process_time = 6.000000

 

posted @ 2021-08-26 23:36  皮特99  阅读(180)  评论(0)    收藏  举报