c++(数据类型 && 自定义set排序,仿函数)

自定义set排序
如果是自定义类型,再插入之前就要定义好,排序规则不然会报错

template<class T,class T2>
void printSet(set<T,T2> &a)
{
	for (auto it = a.begin(); it != a.end(); ++it)
	{
		cout << (*it) << endl;
	}
}
//仿函数
class myComper
{
public:
	//重载()
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void test04()
{
	//从大到小的排序
	//set<int,数据类型> iset = { 1,2,3,5,6,9,4,7,8 };所以这块需要仿函数
	set<int, myComper> iset;
	iset = { 1,2,3,5,6,9,4,7,8 };
	printSet(iset);
}

posted on 2021-04-28 15:20  lodger47  阅读(357)  评论(0)    收藏  举报

导航