C++初阶:容器适配器priority_queue常用接口详解及模拟实现、仿函数介绍

本文详细介绍了C++中的priority_queue,包括其原理、使用方法,以及如何通过模拟实现。重点讲述了priority_queue作为容器适配器的工作机制,以及如何通过自定义函数对象创建不同类型的堆。
摘要由CSDN通过智能技术生成

介绍完了stack和queue的介绍以及模拟的相关内容后:C++初阶:容器适配器介绍、stack和queue常用接口详解及模拟实现
接下来进行priority_queue的介绍以及模拟:



1.priority_queue的介绍和使用

1.1priority_queue的初步介绍

image-20240130191313960

  1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的(默认是大堆)

  2. 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)

  3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。

  4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:

  • empty():检测容器是否为空
  • size():返回容器中有效元素个数
  • front():返回容器中第一个元素的引用
  • push_back():在容器尾部插入元素
  1. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。

  2. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。

1.2priority_queue的使用

image-20240130192200400

函数声明接口说明
priority_queue()构造一个空的优先级队列
priority_queue(first, last)构造一个优先级队列,包含范围为[first, last)的元素
empty()检测优先级队列是否为空,是返回true,否则返回false
top()返回优先级队列中最大(最小)元素,即堆顶元素
push(x)在优先级队列中插入元素x
pop()删除优先级队列中最大(最小)元素,即堆顶元素
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int main()
{
	priority_queue<int> pq;//这里默认是大堆
	pq.push(3);
	pq.push(2);
	pq.push(6);
	pq.push(9);
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;

	priority_queue<int, vector<int>, greater<int>> pq2;//这里传入一个类型greater<int>(马上就讲了)
	pq2.push(3);
	pq2.push(2);
	pq2.push(6);
	pq2.push(9);
	while (!pq2.empty())
	{
		cout << pq2.top() << " ";
		pq2.pop();
	}
	cout << endl;
	return 0;
}

image-20240130194228894

1.3进一步补全介绍

优先队列(priority_queue)是一个特殊的队列,它根据元素的优先级进行排序,而不是按照它们被插入的顺序。在C++中,优先队列通常使用堆(heap)数据结构来实现,这使得它能够在==O( l o g n logn logn)的时间复杂度内对元素进行插入和删除操作,并能够以O(1)的时间复杂度获取队列中的最大(或最小)==元素。

以下是优先队列(priority_queue)的一些重要特性和接口:

  1. 构造函数
    • priority_queue<Type, Container, Compare>:创建一个优先队列对象,其中Type是元素类型,Container是底层容器类型(默认为vector),Compare是元素比较的函数对象类型(默认为std::less,用于最大堆)。
    • priority_queue(first, last):使用范围为[first, last)的迭代器构造一个优先队列。
  2. 默认行为
    • 默认情况下,优先队列是最大堆,即最大元素位于堆顶。可以通过自定义比较函数对象来改变这一行为,从而创建最小堆或者基于自定义的优先级规则进行排序。
  3. 底层实现
    • 在C++中,优先队列通常使用vector或deque作为底层容器,并通过堆算法来维护元素的顺序。

2.仿函数/函数对象讲解

函数对象(Functor)也称为仿函数(Function Object),是C++中的一种重要概念,它是一个行为类似函数的对象,可以被当作函数来调用。在C++中,函数对象可以以类的形式实现(其实是个类)重载operator()运算符,从而可以像函数一样被调用

函数对象可以提供比普通函数更多的灵活性和功能,它可以保存状态、具有成员变量、可以在构造函数中接受参数等。函数对象通常用于STL中的算法、容器和适配器中,它们可以作为参数传递给算法,用于自定义排序、查找、比较等操作。

namespace FunctionObject
{
	template<class T>
	class less
	{
	public:
		bool operator()(const T& a, const T& b)
		{
			return a < b;
		}
	};

	template<class T>
	class greater
	{
	public:
		bool operator()(const T& a, const T& b)
		{
			return a > b;
		}
	};
}

void test1()
{
	int a = 1;
	int b = 10;
	FunctionObject::greater<int> big;//定义一个对象
	cout << big(a, b) << endl;//只看big(a, b) 跟函数调用长得一样
}

int main()
{
	test1();
	return 0;
}

image-20240130195912118


3.模拟priority_queue

文件规划和一览

在这里插入图片描述

priority_queue.h:用来实现queue

test.cpp:进行测试

3.1模拟priority_queue(priority_queue.h)

#pragma once

namespace MyPriority_queue
{
	template<class T>
	class less
	{
	public:
		bool operator()(const T& a, const T& b)
		{
			return a < b;
		}
	};

	template<class T>
	class greater
	{
	public:
		bool operator()(const T& a, const T& b)
		{
			return a > b;
		}
	};

	template<class T, class Container = vector<T>, class Compare = less<T>>//默认是大堆
	class priority_queue
	{
	public:

		void adjust_up(int child)
		{
			Compare com;
			int father = (child - 1) / 2;//得到父节点的索引
			while (child > 0)//再怎么向上也只能到0
			{
				if (com(_con[father], _con[child]))
				{
					swap(_con[father], _con[child]);
					child=father;
					father= (child - 1) / 2;//更新两个节点
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int father)
		{
			Compare com;
			int child = father * 2 + 1;//假设左孩子较大
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))//右孩子存在且比左孩子大
				{
					child++;
				}

				if(com(_con[father], _con[child]))
				{
					swap(_con[father], _con[child]);
					father=child;
					child = father * 2 + 1;//更新两个节点
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);

			adjust_up(_con.size() - 1);
		}

	    //先交换后再向下调整
		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();

			adjust_down(0);
		}

		const T& top()
		{
			return _con[0];
		}

		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}

	private:
		Container _con;
	};
}

3.2测试(test.cpp)

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

#include"priority_queue.h"

int main()
{
	MyPriority_queue::priority_queue<int> pq;//这里默认是大堆
	pq.push(3);
	pq.push(2);
	pq.push(6);
	pq.push(9);
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;

	priority_queue<int, vector<int>, MyPriority_queue::greater<int>> pq2;
	pq2.push(2);
	pq2.push(6);
	pq2.push(9);
	while (!pq2.empty())
	{
		cout << pq2.top() << " ";
		pq2.pop();
	}
	cout << endl;
	return 0;
}

image-20240130210120238


后天开学,今天也是才到学校,正月十五就一人在宿舍过了😭😭😭
到此为止,容器的部分我们大都讲完啦!!!大家敬请期待接下来的知识分享吧!!!

  • 74
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 50
    评论
评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是Nero哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值