草庐IT

【C++】priority_queue、仿函数和反向迭代器

星河万里᭄ꦿ࿐ 2023-04-15 原文

文章目录


一、priority_queue

1. priority_queue的介绍

  1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
  2. 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元
    素)。
  3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。
  4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:
    empty():检测容器是否为空
    size():返回容器中有效元素个数
    front():返回容器中第一个元素的引用
    push_back():在容器尾部插入元素
    pop_back():删除容器尾部元素
  5. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。
  6. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。

2. priority_queue的使用

优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。注意:默认情况下priority_queue是大堆。

void test()
{
	//默认是大堆
	priority_queue<int> pq;
	pq.push(2);
	pq.push(5);
	pq.push(0);
	pq.push(9);
	pq.push(3);
	pq.push(1);
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
	//建立小堆
	priority_queue<int,vector<int>,greater<int>> pq1;
	pq1.push(2);
	pq1.push(5);
	pq1.push(0);
	pq1.push(9);
	pq1.push(3);
	pq1.push(1);
	while (!pq1.empty())
	{
		cout << pq1.top() << " ";
		pq1.pop();
	}
	cout << endl;
}

经典例题


数组中第k大的元素

💕 解题思路

根据堆的性质,我们可以考虑使用数组建大堆,然后pop掉k-1个元素,这时候堆顶的数就是第k大的数,但是这种思路的时间复杂度是O(N+k*logN),夫如果N很大的时候,就需要浪费很多的空间,这里我们还可以考虑第二种做法,就是利用topK的思路,先用前k个数建一个小堆,然后遍历剩下的元素,遇到比堆顶大的元素就替换掉堆顶的元素。注意这里的替换是指,先pop掉堆顶的元素,然后再插入目标元素。这里的时间复杂度是O(k+(N-k)*logk),虽然时间上没有太大的提升,但是空间上却减少了浪费。

💕 解题代码

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        //使用topk问题的求解方式
        priority_queue<int,vector<int>,greater<int>> pq(nums.begin(),nums.begin()+k);
        for(int i = k;i < nums.size();i++)
        {
            if(nums[i] > pq.top())
            {
                pq.pop();
                pq.push(nums[i]);
            }
        }
        return pq.top();
    }
};


二、仿函数

1. 仿函数的使用

仿函数(Functor) 又称为函数对象(Function Object)是一个能行使函数功能的类。仿函数的语法几乎和我们普通的函数调用一样,不过作为仿函数的类,都必须重载 operator() 运算符。因为调用仿函数,实际上就是通过类对象调用重载后的 operator() 运算符。,这里的()指的就是函数调用操作符。

template<class T>
struct Greater {
	bool operator()(const T& x, const T& y)
	{
		return x > y;
	}
};

template<class T>
struct Less {
	bool operator()(const T& x, const T& y)
	{
		return x < y;
	}
};

int main()
{
	Greater<int> gt;
	cout << gt(1, 2) << endl;//相当于gt.operator()(1,2),gt是一个对象,可以像函数一样使用
	cout << Less<int>()(1, 2) << endl;//直接使用匿名对象调用operator(1,2)
	return 0;
}

内置类型使用仿函数

template<class T, class Compare>
void BubbleSort(T* a, int n, Compare com)
{
	for (int j = 0; j < n; j++)
	{
		int exchange = 0;
		for (int i = 1; i < n - j; i++)
		{
			if (com(a[i], a[i - 1]))
			{
				swap(a[i - 1], a[i]);
				exchange = 1;
			}
		}
		if (exchange == 0)
		{
			break;
		}
	}
}

template<class T>
struct Greater {
	bool operator()(const T& x, const T& y)
	{
		return x > y;
	}
};

template<class T>
struct Less {
	bool operator()(const T& x, const T& y)
	{
		return x < y;
	}
};

int main()
{
	Less<int> ls;
	int a[] = { 2,3,4,5,6,1,2,8 };
	BubbleSort(a, sizeof(a) / sizeof(int), ls);
	cout << "升序排序的结果为:";
	for (auto e : a)
	{
		cout << e << " ";
	}
	cout << endl;
	Greater<int> gt;
	BubbleSort(a, sizeof(a) / sizeof(int), gt);
	cout << "降序排序的结果为:";
	for (auto e : a)
	{
		cout << e << " ";
	}
	cout << endl;

	return 0;
}

下面我们看一下仿函数的优缺点:

优点:

  • 在同一时间里,由某个仿函数所代表的单一函数,可能有不同的状态(可以根据不同的类型代表不同的状态)
  • 仿函数即使定义相同,也可能有不同的类型(可以有不同的类型)
  • 仿函数使程序代码变得简单(仿函数在一定程度上使代码更通用,本质上简化了代码)

缺点:

仿函数比一般函数速度慢

自定义类型使用仿函数

前面我们谈到了内置类型,下面我们来看一下日期类这个自定义类型如何使用仿函数:

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	bool operator<(const Date& d)const
	{
		return (_year < d._year) ||
			(_year == d._year && _month < d._month) ||
			(_year == d._year && _month == d._month && _day < d._day);
	}
	bool operator>(const Date& d)const
	{
		return (_year > d._year) ||
			(_year == d._year && _month > d._month) ||
			(_year == d._year && _month == d._month && _day > d._day);
	}
	friend ostream& operator<<(ostream& _cout, const Date& d)
	{
		_cout << d._year << "-" << d._month << "-" << d._day;
		return _cout;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	//建立小堆
	priority_queue<Date,vector<Date>, greater<Date>> q;
	q.push(Date(2023, 1, 1));
	q.push(Date(2023, 4, 7));
	q.push(Date(2020, 1, 1));
	cout << q.top() << endl;
	//建立大堆
	priority_queue<Date> q2;
	q2.push(Date(2023, 1, 1));
	q2.push(Date(2023, 4, 7));
	q2.push(Date(2020, 1, 1));
	cout << q2.top() << endl;

	return 0;
}

自定义类型自己定义仿函数

如果传入的是一个Date*的指针的时候,这个时候仿函数中的运算符重载已经不符合我们的要求了,这个时候我们就需要自己定义仿函数

template<class T>
struct Greater {
	bool operator()(const Date* x, const Date* y)
	{
		return *x > *y;
	}
};

template<class T>
struct Less {
	bool operator()(const Date* x, const Date* y)
	{
		return *x < *y;
	}
};


2. priority_queue的模拟实现

这里的priority_queue就是我们在数据结构阶段讲过的优先级队列——堆,所以我们在这里只需要用类模板和适配器模式来封装一下即可,当然了,这里我们还会用到仿函数来调整堆的构建方式是大堆还是小堆。

namespace cjl
{
	template<class T>
	struct greater {
		bool operator()(const T& x, const T& y) const
		{
			return x > y;
		}
	};

	template<class T>
	struct less {
		bool operator()(const T& x, const T& y) const
		{
			return x < y;
		}
	};

	template<class T, class Container = vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:
		priority_queue()
		{}
		template<class Iterator>
		priority_queue(Iterator first, Iterator last)
		{
			while (first != last)
			{
				push(*first);
				first++;
			}
		}

		//向上调整算法
		void adjust_up(int child)
		{
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (_com(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		//向下调整算法
		void adjust_down(int parent)
		{
			int child = parent * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && _com(_con[child], _con[child + 1]))
				{
					child++;
				}
				if (_com(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 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);
		}

		//取堆中数据个数
		size_t size() const
		{
			return _con.size();
		}

		//取堆顶数据
		const T& top() const
		{
			return _con[0];
		}

		//堆的判空操作
		bool empty() const
		{
			return _con.empty();
		}

	private:
		Container _con;
		Compare _com;
	};
}


三、反向迭代器

1. 反向迭代器的使用

前面我们讲解list的时候对正向迭代器已经有了一个很好的认识,下面我们来认识一下反向迭代器
其中正向迭代器是iteratorconst_iterator,反向迭代器则为:reverse_iteratorconst_reverse_iterator,其实反向迭代器和正向迭代器的区别如下:

  • rbegin()相当于end()
  • rend()相当于begin()
  • 反向迭代器的++相当于正向迭代器–
  • 其他的操作和正向迭代器相同

int main()
{
	int arr[] = { 1,2,3,4,5 };
	list<int> lt(arr, arr + 5);

	list<int>::reverse_iterator rit = lt.rbegin();
	while (rit != lt.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

	return 0;
}


2. 反向迭代器的模拟实现

这里我们可以先来看一下STL库中的反向迭代器是如何实现的,在stl_iterator.h这个头文件中,部分源码如下:

template <class Iterator>
class reverse_iterator {
protected:
	Iterator current;

public:
	typedef Iterator iterator_type;
	typedef reverse_iterator<Iterator> self;

public:
	reverse_iterator() {}
	explicit reverse_iterator(iterator_type x) : current(x) {}
	reverse_iterator(const self& x) : current(x.current) {}
	reference operator*() const {
		Iterator tmp = current;
		return *--tmp;
	}
#ifndef __SGI_STL_NO_ARROW_OPERATOR
	pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */

	self& operator++() {
		--current;
		return *this;
	}
	self& operator--() {
		++current;
		return *this;
	}
	//...
};

这里我们也需要模仿库里面,反向迭代器是一个容器适配器,他的适配器就是正向迭代器,这样它就能根据传递过来的正向迭代器的不同实例化出对应的反向迭代器了。因此,我们只需要将每个容器对应的正向迭代器传递过来,我们就可以实现出对应的反向迭代器了。

namespace cjl
{
	template<class Iterator,class Ref,class Ptr>
	class ReverseIterator
	{
	public:
		typedef ReverseIterator<Iterator, Ref, Ptr> Self;

		//构造函数
		ReverseIterator(const Iterator& it)
			:_cur(it)
		{}

		//重载*
		Ref operator*()
		{
			auto tmp = _cur;
			return *--tmp;
		}
		
		//重载&
		Ptr operator->()
		{
			return &(*_cur);
		}

		//重载前置++
		Self& operator++()
		{
			--_cur;
			return *this;
		}

		//重载后置++
		Self operator++(int)
		{
			auto tmp = _cur;
			_cur--;
			return tmp;
		}

		//重载前置--
		Self& operator--()
		{
			++_cur;
			return *this;
		}

		//重载后置--
		Self& operator--(int)
		{
			auto tmp = _cur;
			_cur++;
			return tmp;
		}

		//重载!=
		bool operator!=(const Self& s)
		{
			return _cur != s._cur;
		}

		//重载==
		bool operator==(const Self& s)
		{
			return _cur = s._cur;
		}
	private:
		Iterator _cur;
	};
}


这里我们可以在我们之前自己模拟实现的list容器中测试一下:


有关【C++】priority_queue、仿函数和反向迭代器的更多相关文章

  1. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  2. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  3. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  4. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  5. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  6. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

  7. ruby-on-rails - 将字符串转换为 ruby​​-on-rails 中的函数 - 2

    我需要一个通过输入字符串进行计算的方法,像这样function="(a/b)*100"a=25b=50function.something>>50有什么方法吗? 最佳答案 您可以使用instance_eval:function="(a/b)*100"a=25.0b=50instance_evalfunction#=>50.0请注意,使用eval本质上是不安全的,尤其是当您使用外部输入时,因为它可能包含注入(inject)的恶意代码。另请注意,a设置为25.0而不是25,因为如果它是整数a/b将导致0(整数)。

  8. ruby - 在 ruby​​ 中使用 .try 函数和 .map 函数 - 2

    我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法

  9. ruby - 是否可以从也在该模块中的类内部调用模块函数 - 2

    在这段Ruby代码中:ModuleMClassC当我尝试运行时出现“'M:Module'的未定义方法'helper'”错误c=M::C.new("world")c.work但直接从另一个类调用M::helper("world")工作正常。类不能调用在定义它们的同一模块中定义的模块函数吗?除了将类移出模块外,还有其他解决方法吗? 最佳答案 为了调用M::helper,你需要将它定义为defself.helper;结束为了进行比较,请查看以下修改后的代码段中的helper和helper2moduleMclassC

  10. ruby - 将运算符传递给函数? - 2

    也许这听起来很荒谬,但我想知道这对Ruby是否可行?基本上我有一个功能...defadda,bc=a+breturncend我希望能够将“+”或其他运算符(例如“-”)传递给函数,这样它就类似于...defsuma,b,operatorc=aoperatorbreturncend这可能吗? 最佳答案 两种可能性:以方法/算子名作为符号:defsuma,b,operatora.send(operator,b)endsum42,23,:+或者更通用的解决方案:采取一个block:defsuma,byielda,bendsum42,23,

随机推荐