c++ std::sort函数调用经常出现的invalidate operator<错误原因以及解决方法
在c++编程中使用sort函数,自定义一个数据结构并进行排序时新手经常会碰到这种错误。

这是为什么呢?原因在于什么?如何解决?
看下面一个例子:
int main(int, char*[])
{
struct ItemDesc
{
int val;
std::string content;
};
std::vector<ItemDesc> dataList = {
ItemDesc{ , "hello" },
ItemDesc{ , "aello" },
ItemDesc{ , "hello" },
ItemDesc{ , "xello" },
ItemDesc{ , "hellx" }
};
std::sort(dataList.begin(), dataList.end(), [](const ItemDesc& lhs, const ItemDesc& rhs){
return (lhs.val <= rhs.val);
});
return ;
}
这段代码在vs2013上就会触发上述断言错误。
std::sort可能在比较的过程中对同一对数据进行多次比较,必须保证同一对数据多次比较结果是一致的。即:
当lhs为ItemDesc{ 0, "hello" },而rhs为ItemDesc{ 0, "hellx" },返回true,第二次比较并且顺序交换后,即ItemDesc{ 0, "hellx" },而rhs为ItemDesc{ 0, "hello" },它应该返回false。
而上述函数无法保证这一点。
所以可以改成下面这样:
int main(int, char*[])
{
struct ItemDesc
{
int val;
std::string content;
};
std::vector<ItemDesc> dataList = {
ItemDesc{ , "hello" },
ItemDesc{ , "aello" },
ItemDesc{ , "hello" },
ItemDesc{ , "xello" },
ItemDesc{ , "hellx" }
};
std::sort(dataList.begin(), dataList.end(), [](const ItemDesc& lhs, const ItemDesc& rhs){
//main filed
if (lhs.val < rhs.val)
{
return true;
}else if (lhs.val > rhs.val)
{
return false;
}
else
{
//compare other data filed
//...
return lhs.content < rhs.content;
}
});
return ;
}
c++ std::sort函数调用经常出现的invalidate operator<错误原因以及解决方法的更多相关文章
- c++中sort函数调用报错Expression : invalid operator <的内部原理 及解决办法
转自:https://www.cnblogs.com/huoyao/p/4248925.html 当我们调用sort函数进行排序时,中的比较函数如果写成如下 bool cmp(const int &a ...
- c++中sort函数调用报错Expression : invalid operator <的内部原理
当我们调用sort函数进行排序时,中的比较函数如果写成如下 bool cmp(const int &a, const int &b) { if(a!=b) return a<b; ...
- 将三维空间的点按照座标排序(兼谈为std::sort写compare function的注意事项)
最近碰到这样一个问题:我们从文件里读入了一组三维空间的点,其中有些点的X,Y,Z座标只存在微小的差别,远小于我们后续数据处理的精度,可以认为它们是重复的.所以我们要把这些重复的点去掉.因为数据量不大, ...
- 源码阅读笔记 - 1 MSVC2015中的std::sort
大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...
- std::sort引发的core
#include <stdio.h> #include <vector> #include <algorithm> #include <new> str ...
- 一个std::sort 自定义比较排序函数 crash的分析过程
两年未写总结博客,今天先来练练手,总结最近遇到的一个crash case. 注意:以下的分析都基于GCC4.4.6 一.解决crash 我们有一个复杂的排序,涉及到很多个因子,使用自定义排序函数的st ...
- Qt使用std::sort进行排序
参考: https://blog.csdn.net/u013346007/article/details/81877755 https://www.linuxidc.com/Linux/2017-01 ...
- 非常无聊——STD::sort VS 基数排序
众所周知,Std::sort()是一个非常快速的排序算法,它基于快排,但又有所修改.一般来说用它就挺快的了,代码一行,时间复杂度O(nlogn)(难道不是大叫一声“老子要排序!!”就排好了么...). ...
- 今天遇到的一个诡异的core和解决 std::sort
其实昨天开发pds,就碰到了core,我还以为是内存不够的问题,或者其他问题. 今天把所有代码挪到了as这里,没想到又出core了. 根据直觉,我就觉得可能是std::sort这边的问题. 上网一搜, ...
随机推荐
- html部分---样式表,选择器;
<1.内联样式,优点:控制精确,缺点:代码重用性差,页面代码乱.> <div style="background-color:#0F0"></div& ...
- ps白平衡
ps白平衡:在正常光线下看起来是白颜色的东西在有色光或者较暗的光线下看起来可能就不是白色,还有荧光灯下的"白"也是"非白".对于这一切如果能调整白平衡,则在所得 ...
- js toggle事件
参数:even (Function): 第奇数次点击时要执行的函数. odd (Function): 第偶数次点击时要执行的函数. 示例:$("p").toggle(functio ...
- java 将长度很长的字符串(巨大字符串超过4000字节)插入oracle的clob字段时会报错的解决方案
直接很长的字符串插入到clob字段中会报字符过长的异常,相信大家都会碰到这种情况 String sql = "insert into table(request_id,table_name, ...
- javascript 遍历object对象
(function(){ var str = ''; for(var i in obj){ //遍历object str += '\n'+(i+' : '+obj[i]); // i+' : '+ob ...
- ExtJs 入门教程
http://www.cnblogs.com/iamlilinfeng/archive/2012/12/31/2840663.html
- Postfix 发送邮件流程简析
PostFix接受和转发邮件的说明 来源ip符合inet_interfaces,收件人域符合mydestination, Postfix将接收到本地. 来源ip符合inet_interfaces, ...
- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter作用
在Spring与Hibernate集成时在web.xml要加入这样的过滤器: <filter> <filter-name>openSessionInView</filte ...
- [IoC]6 详解@Autowired、@Qualifier和@Required
A.@Autowired org.springframework.beans.factory.annotation.Autowired public @interface Autowired Mark ...
- Oracle字符集与客户端
http://www.linuxidc.com/Linux/2011-11/47383p2.htm 什么是Oracle字符集Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关 ...