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部分---格式与布局;
一:position:fixed(相对于浏览器窗口来对元素进行定位) <style type="text/css"> .aa { position:fixed; lef ...
- activiti 任务节点 处理人设置【转】
转自http://blog.csdn.net/qq_30739519/article/details/51225067 1.1.1. 前言 分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自 ...
- MySql中的skip-name-resovle
mysql用的一直很好用,有一次断网了,发现连接虚拟机里的mysql特别费劲,几经扔腾,才知道是因为断网以后,名字解析这块有点问题,在my.cnf文件中加了一条skip-name-resovle,果断 ...
- javascript闭包实例
实例一 //每次执行一次c()i加1.关键在于var c=a():c容器将i装载记住了. function a(){ var i=0; function b(){ alert(++i); } retu ...
- vsftp虚拟用户配置
找了很久,终于找到像样一点的文章,很详细,参数方面懂英文基本能看懂,一个教程是否有用,关键在于细节.错了一点点就不能配下去了. ------------------------------------ ...
- 对象属性操作-包含kvc---ios
#import <Foundation/Foundation.h> @class Author; @interface Books : NSObject{ @private NSStrin ...
- 在Discuz中增加创始人
第一步 在 /config/config_uccenter.php 中 修改 $_config['admincp']['founder'] = '用户UID,用户UID2'; 第二步 在 UPDA ...
- 虚拟化之vmware-网络
http://blog.sina.com.cn/s/blog_6b89db7a01012jtw.htmlESXI 5.0 虚拟机的网络适配器兼容性列表 就需要在vSphere标准交换机(vSphere ...
- 常用邮件 smtp pop
常用的邮箱服务器(SMTP.POP3)地址.端口 sina.com: POP3服务器地址:pop3.sina.com.cn(端口:110) SMTP服务器地址:smtp.sina.com.cn(端口 ...
- selenium+python自动化之元素定位
自动化按步骤拆分的话,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告.本篇接下来讲基本的八种元素定位方法.说的通俗一点,就是教大家找对象. ...