set/priority_queue的运算符重载

#include<bits/stdc++.h>
using namespace std;
struct cmp
{
	bool operator ()(int a, int b) //重载小于
	{ return a > b; }
};
set<int,cmp>s1; //递增
set<int>s2; //递减
struct myint
{
	int val;
	myint(int v):val(v){}
	friend bool operator <(myint a, myint b) //重载小于
	{ return a.val > b.val; }
};
set<myint>s3; //递增
int main ()
{
	s1.insert(5);
	s1.insert(6);
	s1.insert(7);
	s1.insert(2);
	set<int,cmp>::iterator it;
	for(it = s1.begin(); it != s1.end(); it++) //递增
		printf("%d ", *it);
	s2.insert(5);putchar(10);
	s2.insert(6);
	s2.insert(7);
	s2.insert(2);
	set<int>::iterator itt;
	for(itt = s2.begin(); itt != s2.end(); itt++) //递减
		printf("%d ", *itt);
	s3.insert(myint(5));putchar(10);
	s3.insert(myint(6));
	s3.insert(myint(7));
	s3.insert(myint(2));
	set<myint>::iterator ittt;
	for(ittt = s3.begin(); ittt != s3.end(); ittt++) //递增
		printf("%d ", (*ittt).val);
}

set默认从小到大排序,只要重载<就行了

priority_queue默认从大到小排序,也只要重载<

 

posted @ 2019-12-14 14:52  _Ark  阅读(71)  评论(0编辑  收藏  举报