优先级队列 priority_queue 与 仿函数 greater / less

@TOC

优先级队列 priority_queue 与 仿函数 greater / less

仿函数

定义及优点

定义:定义一个类,类里面重载函数运算符(),将该类的对象作为函数的入参,那么在函数中同样能调用重载符()里面的方法。

优点:相比于普通函数,仿函数的声明更为简单。

例如:用普通函数,仿函数写一个swap函数,并将其以参数的形式,如下:

cpp 复制代码
//普通函数
template<class T>
void _swap(T& a, T& b)
{ T c = a; a = b; b = c; }
//声明参数
void(*p)(int&, int&) = _swap;

再来看看仿函数写法:
```cpp
//仿函数
template<class T>
struct _swap
{
    void operator()(T& a, T& b)
	{ T c = a; a = b; b = c; }
};
//声明参数
_swap<int> p;

我们可以看到函数指针,在面向对象开发的过程中编写有点繁琐,所以C++的仿函数借此代替了函数指针。

有了上面理解的铺垫就可以,很轻松的理解下面两个仿函数了。

greater

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

less

cpp 复制代码
template<class T>
class less
{
public:
	bool operator()(const T& a, const T& b)
	{
		return a < b;
	}
};

优先级队列

priority_queue

定义:优先级队列的本质其实是一个堆 ,插入数据时内部按照所给的仿函数为参考进行(堆)排序,确保第一个元素为最值,存在默认的缺省值,一般采用vector为适配器,Less为仿函数.

如下:

cpp 复制代码
template <class T, class Container = vector<T>, class Compare = less<T>>

构造/类的成员

cpp 复制代码
template <class T, class Container = vector<T>, class Compare = less<T>>
class priority_queue
{
public:
	priority_queue()
	{ }
	template <class InputIterator>
	priority_queue(InputIterator first, InputIterator last)
	{
		InputIterator it = first;
		while (it != last)
		{
			push(*it);
			it++;
		}
		//循环遍历插入即可
	}
private:
	Container c;
	Compare comp;
);

Compare是仿函数的类型,Container是适配器的类型 。

这里我们采用的是适配器模式,在对象生命周期结束时,会自动调用默认的构析函数。因为适配器为自定义类型,所以又会自动调用适配器的构析函数,所以我们不需要写构析函数,同理拷贝构造也可以不写。

这里我们写了一个以迭代器为参数的构造函数,所以还是要写一个无参的默认构造函数,什么事都不用做,因为适配器会自动调用它的构造函数。

插入函数 push(const T& x)

插入函数的实现就是利用适配器的插入,再将其插入的数进行向上调整即可。

再向上调整的过程中利用反函数的返回结果作为参考。

cpp 复制代码
void AdjustUp(int child)
{
	int father = (child - 1) / 2;
	while (father >= 0)
	{
		if (comp(c[father], c[child]))
		{
			swap(c[father], c[child]);
			child = father;
			father = (child - 1) / 2;
		}
		else break;
	}
}

void push(const T& x)
{
	c.push_back(x);
	AdjustUp(c.size() - 1);
}

删除函数 pop()

删除函数和堆的删除相同。

cpp 复制代码
void AdjustDown(int father)
{
	int child = father * 2 + 1, n = c.size();
	while (child < n)
	{
		if (child + 1 < n && comp(c[child], c[child + 1])) child++;
		if (comp(c[father], c[child]))
		{
			swap(c[father], c[child]);
			father = child;
			child = father * 2 + 1;
		}
		else break;
	}
}

void pop()
{
	swap(c[0], c[c.size() - 1]);
	c.pop_back();
	AdjustDown(0);
}

其他接口

其他功能可以直接通过适配器的调用即可.

cpp 复制代码
bool empty() const
{
	return c.empty();
}
size_t size() const
{
	return c.size();
}
const T& top() const
{
	return c[0];
}

源代码

源代码如下:

cpp 复制代码
	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:
		priority_queue()
		{

		}
		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			InputIterator it = first;
			while (it != last)
			{
				push(*it);
				it++;
			}
		}
		bool empty() const
		{
			return c.empty();
		}
		size_t size() const
		{
			return c.size();
		}
		const T& top() const
		{
			return c[0];
		}

		void AdjustUp(int child)
		{
			int father = (child - 1) / 2;
			while (father >= 0)
			{
				if (comp(c[father], c[child]))
				{
					swap(c[father], c[child]);
					child = father;
					father = (child - 1) / 2;
				}
				else break;
			}
		}

		void push(const T& x)
		{
			c.push_back(x);
			AdjustUp(c.size() - 1);
		}

		void AdjustDown(int father)
		{
			int child = father * 2 + 1, n = c.size();
			while (child < n)
			{
				if (child + 1 < n && comp(c[child], c[child + 1])) child++;
				if (comp(c[father], c[child]))
				{
					swap(c[father], c[child]);
					father = child;
					child = father * 2 + 1;
				}
				else break;
			}
		}

		void pop()
		{
			swap(c[0], c[c.size() - 1]);
			c.pop_back();
			AdjustDown(0);

		}

	private:
		Container c;
		Compare comp;

	};
结语

以上就是本期的全部内容了,喜欢就多多关注吧!!!

相关推荐
王老师青少年编程2 小时前
2025年【江苏“信息与未来”编程思维】真题及题解(T2:坐标变换)
c++·题解·真题·坐标变换·编程思维·江苏·信息与未来
西门吹-禅4 小时前
java springboot N+1问题
java·开发语言·spring boot
skywalk81635 小时前
设计并实现段言的 C FFI 绑定机制 @Trae
c语言·开发语言·python·编程
2023自学中6 小时前
C++ 内存追踪器
linux·c++
IT笔记6 小时前
【Rust】Rust Match 模式匹配详解
java·开发语言·rust
2zcode6 小时前
免费开源项目文档:基于MATLAB卷积神经网络的口罩佩戴检测系统
开发语言·matlab·cnn
逝水无殇7 小时前
C# 运算符重载详解
开发语言·后端·c#
TPBoreas7 小时前
配置信息防泄露方案:.env 环境隔离详解(dotenv-java)
java·开发语言
敲代码的嘎仔7 小时前
实习日志day6--实习日志day6--title命名规范化&businessType纠正&补充缺失的@Log注解&报警与通信模块补充&产出阶段总结文档
java·开发语言·人工智能·git·python·实习·大二
江华森8 小时前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python