Assertion Failure When Sorting STL Vector using Custom Predicate
2015-11-17 10:46
513 查看
程序在运行过程中出现断言错误"invalid operator <",这是因为在vs2005之后的版本出现了strict weak ordering,要求operator <在处理==的情况需要返回false
https://support.microsoft.com/en-us/kb/949171
It throws and assertion failed error, "Expression: invalid operator < " in Visual C++ 2005 and Visual C++ /2008.
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).
First Option:
Second Option:
https://support.microsoft.com/en-us/kb/949171
Action
Sort any STL collection using stable_sort() or sort() with duplicate items using the following custom predicatebool CustPredicate (int elem1, int elem2 ) { if(elem1 > elem2) return true; if (elem1 < elem2) return false; return true; }
Result
It works fine in Visbual 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 issueFirst Option:
bool CustPredicate (int elem1, int elem2 ) { if(elem1 > elem2) return true; if (elem1 < elem2) return false; return false; //Should return false if both the vaules are same }
Second Option:
bool CustPredicate (int elem1, int elem2 ) { return elem1 > elem2; }
相关文章推荐
- [CFString retain]: message sent to deallocated instance”
- ADB server didn't ack * failed to start daemon及unable to obtain result of 'adb versio错误
- AEAI DP开发平台升级说明
- saltstack中grains简介
- A child container failed during start
- AEAI DP开发平台升级说明
- AEAI CRM客户关系管理升级说明
- AEAI CRM客户关系管理升级说明
- 怎样用ZBrush®中功能插件PaintStop
- 一款 jetbrains系列软件的配色插件 Material Theme, 支持sublime
- Linux下线程的调度策略与优先级(一) http://blog.csdn.net/dongliqiang2006/article/details/5837824
- Linux下线程的调度策略与优先级(二) http://blog.csdn.net/dongliqiang2006/article/details/5837828
- UVA 10494 If We Were a Child Again(大数)
- Daily Scrum - 11/16
- hdu 1022 Train Problem I(栈的模拟)
- 《leetCode》:Container With Most Water
- You can fail at what you don't want, so you might as well take a chance on doing what you love.
- 【论文笔记】The Impact of Imbalanced Training Data for CNN
- 关于《游戏编程中的人工智能》的源代码的小错误
- IO(一):传统IO(基于字符,字节,Socket) 与BIO,NIO,AIO 介绍