关于VS2008/2010中SORT,stable_sort的比较函数中strict weak ordering

在VS2008/2010中SORT,stable_sort的比较函数是strict weak ordering。当比较的时候出现元素相等的情况是编译器默认必须返回false,而如果在自定义比较函数时,将相等返回true。将会出现invalid operator<的异常。

注意:这种异常在DEVCPP中时不会出现的。

如:

1 bool cmp(datatype x,datatype y)
2 {if(x>y)
3  return true;
4 if(x==y)
5 return true;//x==y,返回true,将出现invalid operator<异常
6 return false;
7 }

 

所以当比较出现相等的时候必须返回false;

 

即:

bool cmp(datatype x,datatype y)
{
if(x>y)
return true;
if(x<y)
return false;

return false;//当x==y时,返回false;
}

微软官方说明:http://support.microsoft.com/kb/949171

Assertion Failure When Sorting STL Vector using Custom Predicate

Article ID: 949171 - View products that this article applies to.
Source: Microsoft Support
 

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
 

Action

 Sort any STL collection using stable_sort() or sort() with duplicate items using the following custom predicate


1 bool CustPredicate (int elem1, int elem2 )
2 {
3     if(elem1 > elem2)
4         return true;
5 
6     if (elem1 < elem2)
7         return false;
8     return true;
9 }

 









 

Result

It works fine in Visual C++ 2002 and Visual C++ 2003.
It throws and assertion failed error, "Expression: invalid operator < " in Visual C++  2005 and Visual C++ /2008.
 

Cause



The STL algorithms for stable_sort() and sort() require the binary predicate to be strict weak ordering.

For example:

· Strict: pred(X, X) is always false.

· Weak: If !pred(X, Y) && !pred(Y, X), X==Y.

· Ordering: If pred(X, Y) && pred(Y, Z), then pred(X, Z).







Resolution

Below either of the options will resolve the issue

First Option:-

 1 bool CustPredicate (int elem1, int elem2 )
 2 {
 3     if(elem1 > elem2)
 4         return true;
 5 
 6     if (elem1 < elem2)
 7         return false;
 8 
 9     return false; //Should return false if both the vaules are same
10 }

 




Second Option:-
1 bool CustPredicate (int elem1, int elem2 )
2 {
3     return elem1 > elem2;
4 }

 









 

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.
 

Properties

Article ID: 949171 - Last Review: February 13, 2008 - Revision: 1.0
APPLIES TO
  • Microsoft Visual Studio 2005 Express Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Service Pack 1
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Team Suite
  • Microsoft Visual Studio 2008 Academic Edition
  • Microsoft Visual Studio 2008 Professional Edition
  • Microsoft Visual Studio 2008 Standard Edition
  • Microsoft Visual Studio Team System 2008 Team Suite
  

转载于:https://www.cnblogs.com/zjushuiping/archive/2012/08/08/2629038.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值