C ++ std :: set比较器 [英] C++ std::set comparator
问题描述
这是代码:
struct comp
{
bool operator()(Reputation *one, Reputation *two)
{
if (one->Amount < 0 && two->Amount >= 0)
return false;
if (one->Amount >= 0 && two->Amount < 0)
return true;
if (one->Amount >= 0)
return one->Amount <= two->Amount;
else
return one->Amount >= two->Amount;
}
};
这是问题:
调试失败!
文件:..\VC\include\xtree
行:638
Debug Assertion Failed!
File: ..\VC\include\xtree
Line: 638
表达式:无效运算符
Expression: invalid operator<
之后,我可以选择Abort,Retry 。
After that, I can choose "Abort", "Retry" or "Ignore". If I choose ignore many more (identical ones) come up but it ends up working perfectly.
当我插入一个声望的时候,问题似乎发生 - > Amount ==
The problem seems to occur when I insert a Reputation with ->Amount == to one of the Reputation *'s previously inserted, but I'm not sure about this last one.
任何帮助将非常感谢
编辑:我想他们排序的顺序是先按顺序排列的正数,然后是排序的负数。示例:1 5 10 11 11 20 50 -1 -5 -50
The order I want them ordered in is first the positive ones in asc order, then the negative ones in desc order. Example: 1 5 10 11 11 20 50 -1 -5 -50
推荐答案
您必须定义 irreflexive ,就像<
- 因此,将< =
更改为<
和'> ='到'>'。这是VC ++诊断的。
You must define a relation that's irreflexive, just like <
-- therefore, change the <=
to <
and the '>=' to '>' in the last couple of comparisons in your method. This is what VC++ is diagnosing.
此外,给定一个正确的编码,< -
项目a和b使得a& b和b < a都是假的,那些项目被认为是等同的,因此只有一个将被插入到集合中(不是物质是否可以通过一些其他比较来区分项目:只有比较器暗示的等价关系重要)。
Moreover, given a correctly coded, <-
like operator, if two items a and b are such that a < b and b < a are both false, those items are considered equivalent and thus only one will be inserted in the set (it's not material whether the items could be distinguished by some other comparison: only the equivalence relationship implied by the comparator matters).
这篇关于C ++ std :: set比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!