struct compare
{
bool operator()(const pair<string,int>&p1,const pair<string,int>&p2)
{
return p1.second>p2.second;//second为value ,从大到小的顺序
}
}
struct compare1
{
bool operator()(const pair<string,int>&p1,const pair<string,int>&p2)
{
return p1.second<p2.second;//second为value ,从小到大的顺序
}
}
//排序
sort(vec.begin(),vec.end(),compare());//调用自己的比较器函数
struct CMP
{
public:
bool operator() (const int a,const int b)
{
if(a>b)
{
return true; // 从大到小
}else
{
return false;
}
}
};
bool CMP1 (const int a,const int b)
{
if(a<b)
{
return true; // 从小到大
}else
{
return false;
}
}
STL 自定义比较器
最新推荐文章于 2025-10-31 17:08:07 发布
本文介绍了如何使用自定义比较器进行数据结构的排序,并提供了具体的C++实现示例,包括不同类型的比较逻辑。
915

被折叠的 条评论
为什么被折叠?



