C++中优先队列的基本使用(基本类型&自定义类型)

优先队列(priority_queue)定义在头文件< queue > 中,可以用来实现二叉堆(大顶堆、小顶堆),主要使用包括对于基本类型和自定义类型的排序,顶部元素(优先级最高/最低)访问,出队、入队等操作。

  1. 对于基本类型,可以修改默认排序规则,将大顶堆该为小顶堆。
  2. 对于自定义数据类型,可以创建操作符重载函数(重载<符号),或者自定义比较函数(重载()符号),用于自定义类型的比较操作。

接下来给出优先队列的基本使用:

test_01() : 使用int类型
test_02(): 使用string类型
test_03(): 使用自定义类型struct student

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

// 优先队列:基本数据类型的使用 - int
void test_01()
{
	// 默认为大顶堆
	priority_queue<int> p1;
	p1.push(10);
	p1.push(-10);
	p1.push(100);

	cout << "p1 list 大顶堆(int): ";
	while (p1.size())
	{
		cout << p1.top() << " ";
		p1.pop();
	}
	cout << endl;

	// 改为小顶堆实现
	priority_queue<int, vector<int>, greater<int>> p2;
	p2.push(10);
	p2.push(-10);
	p2.push(100);

	cout << "p2 list 小顶堆(int): ";
	while (p2.size())
	{
		cout << p2.top() << " ";
		p2.pop();
	}
	cout << endl;
}

// 优先队列:基本数据类型的使用 - string
void test_02()
{
	// 默认为大顶堆: C B A
	priority_queue<string> p3;
	p3.push("A");
	p3.push("B");
	p3.push("C");

	cout << "p3 list 大顶堆(string): ";
	while (p3.size())
	{
		cout << p3.top() << " ";
		p3.pop();
	}
	cout << endl;

	// 改为小顶堆实现:A B C
	priority_queue<string, vector<string>, greater<string>> p4;
	p4.push("C");
	p4.push("B");
	p4.push("A");

	cout << "p4 list 小顶堆(string): ";
	while (p4.size())
	{
		cout << p4.top() << " ";
		p4.pop();
	}
	cout << endl;
}

// 优先队列:自定义数据类型的使用 - struct
struct student
{
	int score;
	string name;
	int student_number;

	// 重载 < 运算符,用于优先级比较, 以成绩来排名
	bool operator<(const student& s) const
	{
		return this->score < s.score;	// 大顶堆
	}

	student(int s, string n, int sn) :score(s),name(n),student_number(sn){}
};

// 自定义比较函数
struct student_compare_score_greater
{
	bool operator() (const student& a, const student& b)
	{
		return a.score > b.score;	// 小顶堆
	}
};

void test_03()
{
	student s1(89, "wang", 1001001);
	student s2(76, "Li", 1001721);
	student s3(100, "Zhao", 1001321);

	priority_queue<student> p5;
	p5.push(s1);
	p5.push(s2);
	p5.push(s3);

	cout << "p5 list 大顶堆(student): " << endl << endl;
	while (p5.size())
	{
		cout << p5.top().name << " " << p5.top().score << " " << p5.top().student_number << endl;;
		p5.pop();
	}
	cout << endl;

	// 改为成绩由低到高
	priority_queue<student, vector<student>, student_compare_score_greater> p6;
	p6.push(s1);
	p6.push(s2);
	p6.push(s3);

	cout << "p6 list 小顶堆(student): " << endl;
	while (p6.size())
	{
		cout << p6.top().name << "  " << p6.top().score << "  " << p6.top().student_number << endl;;
		p6.pop();
	}
	cout << endl;
}

int main()
{
	// int
	test_01();
	cout << endl;

	// string
	test_02();
	cout << endl;
	
	// struct
	test_03();

	return 0;
}

运行结果如下:

p1 list 大顶堆(int): 100 10 -10
p2 list 小顶堆(int): -10 10 100

p3 list 大顶堆(string): C B A
p4 list 小顶堆(string): A B C

p5 list 大顶堆(student):

Zhao 100 1001321
wang 89 1001001
Li 76 1001721

p6 list 小顶堆(student):
Li  76  1001721
wang  89  1001001
Zhao  100  1001321

谢谢阅读

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C++,我们可以通过自定义比较函数来实现自定义优先队列的排序。比较函数是一个返回布尔值的函数,用于确定元素的优先级。在优先队列,元素按照默认的排序规则进行排序,即大根堆或小根堆。默认情况下,元素按照从大到小的顺序排列,也就是大根堆。如果要实现不同的排序规则,我们可以自定义比较函数来改变元素的排序顺序。 下面是一个示例代码,展示了如何在C++自定义优先队列的排序: ```cpp #include <iostream> #include <queue> using namespace std; // 自定义比较函数,实现从小到大的排序规则 struct Compare { bool operator() (int a, int b) { return a > b; } }; void custom_priority_queue_sort() { int source_data = {3, 5, 8, 1, 10, 2, 9, 15, 13, 16}; priority_queue<int, vector<int>, Compare> q; // 使用自定义的比较函数 for (auto n : source_data) q.push(n); while (!q.empty()) { cout << q.top() << endl; q.pop(); } } int main() { custom_priority_queue_sort(); return 0; } ``` 在上述代码,我们定义了一个名为`Compare`的结构体,其重载了小括号运算符`()`。在自定义的比较函数,我们将元素按照从小到大的顺序排列,即返回`a > b`。然后在创建优先队列时,我们通过指定自定义比较函数`Compare`来改变排序规则。 运行以上代码,输出结果为: ``` 1 2 3 5 8 9 10 13 15 16 ``` 以上代码演示了如何在C++自定义优先队列的排序规则。通过自定义比较函数,我们可以实现各种不同的排序方式来满足特定需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值