0
点赞
收藏
分享

微信扫一扫

【C++STL】set自定义排序,仿函数

一枚路过的程序猿 2022-02-13 阅读 47
c++
struct nnum{
    //重载()
    bool operator () (int v1,int v2){
        return v1>v2;
    }
};

set<int,nnum> num;//仿函数对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);
}
举报

相关推荐

0 条评论