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这边的问题. 上网一搜, ...
随机推荐
- Codeforces Round #146 (Div. 2)
A. Boy or Girl 模拟题意. B. Easy Number Challenge 筛素数,预处理出\(d_i\). 三重循环枚举. C. LCM Challenge 打表找规律. 若\(n\ ...
- LeetCode(228) Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Kali安装
Kali Linux 前身是 BackTrack ,不过Kali Linux是基于Debian 的 Linux 发行版,而BackTrack 则是基于Uubntu的,现在BackTrack 已经不更新 ...
- 越狱Season 1-Episode 7: Riots, Drills and the Devil: Part 2
Season 1, Episode 7: Riots, Drills and the Devil: Part 2 -Pope: Belick, get those guys in line guy: ...
- Android dip(dp) 与 sp的自适应问题
本文转载于:http://www.oschina.net/question/272860_70761 今天碰到的一个问题,感觉应该其他人也会碰到,拿来分享一下. 我们都知道android在开发配置界面 ...
- 翻译:Universal Image Loader
本文转载于:http://blog.csdn.net/tianxiangshan/article/details/9399183 All manipulations are held by the I ...
- 论文阅读之 DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation
DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation Xia ...
- mysql SQL SERVER 的算法
Filesort Probes http://dev.mysql.com/doc/refman/5.7/en/dba-dtrace-ref-filesort.html http://dev.mysql ...
- log tree(merge)
http://www-users.cs.umn.edu/~he/diff/p256-severance.pdf http://www.eecs.harvard.edu/~margo/cs165/pap ...
- Linux-理解ARP、网关、路由
假设你叫小不点(本地主机),住在一个大院子(本地局域网)里,有很多邻居(网络邻居),门口传达室有个看大门的李大爷,李大爷就是你的网关.当你想跟院子里的某个伙伴玩,只要你在院子里大喊一声他的名字(pin ...