我想使用 STL 设置为:
struct
在这个集合中插入一个,比如struct {int id, string info};
我尝试了以下代码:
#include <iostream>
#include <set>
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<(const struct song & right)const
{
if(this->m_id == right.m_id) { // remove duplicated
return false;
}
if(this->m_hot != right.m_hot)
{
return this->m_hot > right.m_hot;
}
else
{
return this->m_id > right.m_id;
}
}
};
int main()
{
std::set<song> mySet;
song s1(10,100);
song s2(40,700);
song s3(40,300);
song s4(30,200);
song s5(300,200);
song s6(300,900);
mySet.insert(s1);
mySet.insert(s2);
mySet.insert(s3);
mySet.insert(s4);
mySet.insert(s5);
mySet.insert(s6);
for(auto it:mySet)
{
std::cout<<"id:"<<it.m_id<<",hot:"<<it.m_hot<<std::endl;
}
}
输出是:
id:300,hot:900
id:40,hot:700
id:300,hot:200
id:30,hot:200
id:10,hot:100
如您所见,id 40 成功删除了重复项,但 300 失败了。我认为 id 300 部分不应该出现两次,有人可以帮我吗?
你operator<
不满足严格弱排序,你的要求不能这样表达。
问题是您有两个键,m_id
(用于唯一性)和m_hot
(用于排序)。您不能选择仅按 订购m_hot
。您总是必须按两者排序(并忽略等效比较元素),并且您只能选择优先级。因此,您无法在{id=0, hot=0}
和之间建立等价关系{id=0, hot=1}
。
^ 如果你的比较器不能写成std::tie(lhs.keys...) < std::tie(rhs.keys...)
,那就不好了。
目前,我只能考虑m_hot
在您需要的地方推迟订购(通过复制到 astd::vector
并对其进行排序或插入到std::set
具有不同比较器的 a )。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句