c++语言定义排序函数,c++中std::set自定义去重和排序函数

本文详细介绍了C++中std::set容器的使用,重点讲解了如何利用内置的less函数进行基本的排序去重,以及如何自定义比较函数以满足结构体对象的去重和排序需求。实例演示了结构体song的id去重和hot降序排序的过程。
摘要由CSDN通过智能技术生成

c++中的std::set,是基于红黑树的平衡二叉树的数据结构实现的一种容器,因为其中所包含的元素的值是唯一的,因此主要用于去重和排序。这篇文章的目的在于探讨和分享如何正确使用std::set实现去重和排序功能。

方法一:使用std::set内置的less比较函数(直接定义内置类型的set对象)

这种方法适用于:1)比较int、char等内置类型。2)只能针对某一个内置类型去重和排序:如果想通过id(int)去重,并通过hot(int)排序,该种方法就束手无策了。代码如下:

#include

#include

using namespace std;

void main()

{

std::set mySet; // 直接定义内置类型set集合

mySet.insert(10); // 默认比较函数为less

mySet.insert(20); // 从小到大排序

for(auto it:mySet)

{

std::cout<

}

std::cout<

}

结果如下:

output:

10

20

end

方法二:自定义类(结构体)比较函数

前文提到:直接定义内置类型的set对象,即使用std::set内置的默认的less比较函数,可能不能满足我们的实际需求。例如:现在有一批结构体对象,需要将其插入set集合,并按照id去重,按照热度hot进行排序。这个时候,就需要重新自定义比较函数了。有两种方法可以自定义比较函数:

重载

为甚么要重载=”运算符?答案是不可以。几乎所有的方法或容器都需要排序来满足数学意义上的标准严格弱序化,否则这些方法或容器的行为将不可预知。假设f(x,y)是一个比较函数。 如果该函数满足如下条件则它是严格弱序化的。

1. f(x,x) = false;

2. if f(x,y) then !f(y,x)

3. if f(x,y) and f(y,z) then f(x,z)

4. if !f(x,y)&&!f(y,x) then x==y; if x==y and y==z then x==z;

看上去有点晕乎,不过不用担心,只要你的比较方法能够满足对相等元素永远返回false(记住一个准则:永远让比较函数对相同元素返回false),那你的方法就满足要求了。

其实,set容器在判定已有元素a和新插入元素b是否相等时,是这么做的:

1)将a作为左操作数,b作为有操作数,调用比较函数,并返回比较值

2)将b作为左操作数,a作为有操作数,再调用一次比较函数,并返回比较值。

如果1、2两步的返回值都是false,则认为a、b是相等的,则b不会被插入set容器中

如果1、2两步的返回值都是true,则可能发生未知行为

因此,记住一个准则:永远让比较函数对相同元素返回false。

#include

#include

using namespace std;

struct song

{

int m_id;

int m_hot;

song(int id,int hot)

{

this->m_id = id;

this->m_hot = hot;

}

bool operator

{

if(this->m_id == right.m_id) //根据id去重

return false;

else

{

if(this->m_hot != right.m_hot)

{

return this->m_hot > right.m_hot; //降序

}

else

{

return this->m_id > right.m_id;

}

}

}

};

void main()

{

std::set mySet;

song s1(10,100);

song s2(20,200);

song s3(20,300);

song s4(30,200);

mySet.insert(s1); //插入s1

mySet.insert(s2); //插入s2

mySet.insert(s3); //s3和s2的id相同,不插入

mySet.insert(s4); //插入s4

for(auto it:mySet)

{

std::cout<

}

std::cout<

};

结果如下:

id:30,hot : 200

id:20,hot : 200

id:10,hot : 100

end

重载()运算符

具体代码如下:

#include

#include

using namespace std;

struct song

{

int m_id;

int m_hot;

song(int id,int hot)

{

this->m_id = id;

this->m_hot = hot;

}

/* bool operatorm_id == right.m_id) //根据id去重 return false; else { if(this->m_hot != right.m_hot) { return this->m_hot > right.m_hot; //降序 } else { return this->m_id > right.m_id; } } } */

};

struct comp

{

bool operator()(struct song left,struct song right) //重载()运算符

{

if(left.m_id == right.m_id) //根据id去重

return false;

else

{

if(left.m_hot != right.m_hot)

{

return left.m_hot > right.m_hot; //降序

}

else

{

return left.m_id > right.m_id;

}

}

}

};

void main()

{

std::set mySet; //写法和2.1中的的区别

song s1(10,100);

song s2(20,200);

song s3(20,300);

song s4(30,200);

mySet.insert(s1); //插入s1

mySet.insert(s2); //插入s2

mySet.insert(s3); //s3和s2的id相同,不插入

mySet.insert(s4); //插入s4

for(auto it:mySet)

{

std::cout<

}

std::cout<

};

复制代码

结果如下:

id:30,hot : 200

id:20,hot : 200

id:10,hot : 100

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值