c++中sort函数调用报错Expression : invalid operator <的内部原理 及解决办法
转自:https://www.cnblogs.com/huoyao/p/4248925.html
当我们调用sort函数进行排序时,中的比较函数如果写成如下
bool cmp(const int &a, const int &b)
{
if(a!=b)
return a<b;
else
return true;
}
则在待排序列中如果出现相等元素,则会报错Expression : invalid operator <
原因是,c++编译器检测到调用cmp的参数a==b时,c++编译器会立即用反序参数调用cmp函数,即调用cmp(b,a),来判断cmp函数是否已经执行了严格的弱序规则(a与b相等则位置不被改变)
注意:两次cmp函数的调用后,都会调用内部的严格弱序规则检测函数(源码如下)
template<class _Pr, class _Ty1, class _Ty2> inline
bool _Debug_lt_pred(_Pr _Pred,
_Ty1& _Left, _Ty2& _Right,
_Dbfile_t _File, _Dbline_t _Line)
{ // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
if (!_Pred(_Left, _Right))
return (false);
else if (_Pred(_Right, _Left))
_DEBUG_ERROR2("invalid operator<", _File, _Line);
return (true);
}
两次上述检测函数的调用,都会因为_Pre函数(即cmp函数)返回true而调用_DEBUG_ERROR2函数。但是_DEBUG_ERROR2有一个特点:只在第二次被迭代调用时输出错误信息。所以,当第二次反序调用cmp时(即cmp(b,a)),程序会报错:Expression : invalid operator <。
综上所述,cmp函数的改进写法如下
bool cmp(const int &a, const int &b)
{
if(a!=b)
return a<b;
else
return false;
}
better version:
bool cmp(const int &a, const int &b)
{
return a<b;
}
c++中sort函数调用报错Expression : invalid operator <的内部原理 及解决办法的更多相关文章
- c++中sort函数调用报错Expression : invalid operator <的内部原理
当我们调用sort函数进行排序时,中的比较函数如果写成如下 bool cmp(const int &a, const int &b) { if(a!=b) return a<b; ...
- 解决MyEclipse中的js报错的小方法
今天,下了个模版,但是导进去的时候发现js会报错.看了下其他都没有错误.而有一个js报错误,请原谅我有点红色强迫症,不能留一点红色 . 错误如下:Syntax error on token " ...
- Eclipse和MyEclipse使用技巧--解决MyEclipse中的js报错的小方法
今天,下了个模版,但是导进去的时候发现js会报错.看了下其他都没有错误.而有一个js报错误,请原谅我有点红色强迫症,不能留一点红色 . 错误如下:Syntax error on token " ...
- Jade报错:Invalid indentation,you can use tabs or spaces but not both问题
现象:通过html生成jade文件之后,更改jade文件时,语句没什么问题的情况下,jade文件编译不通过,报错:Invalid indentation,you can use tabs or spa ...
- mysql创建表时,设置timestamp DEFAULT NULL报错1067 - Invalid default value for 'updated_at'
问题背景: 线上的linux服务器上的mysql服务器中导出数据库的结构.想要在本地创建一个测试版本 导出后再本地mysql上运行却报错 1067 - Invalid default value ...
- K.O. -------- Eclipse中Maven的报错处理
----------------------siwuxie095 K.O. -------- Eclipse 中 Maven 的报错处理 ...
- CocoaPods pod install的时候报错:invalid byte sequence in UTF-8 (ArgumentError)解决办法
CocoaPods pod install的时候报错:invalid byte sequence in UTF-8 (ArgumentError)解决办法: 基本可以确定是Podfile中的内容编码有 ...
- mybatis报错:Invalid bound statement (not found)
mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...
- Springboot项目下mybatis报错:Invalid bound statement (not found)
mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...
随机推荐
- nginx模块之ngx_http_upstream_module
ngx_http_upstream_module 示例: http上下文: upstream upservers{ ip_hash; //根据客户端IP进行调度,每个客户端ip地址访问时每个ip生成一 ...
- mac 非安全模式 开启
https://github.com/callmelaoda/communicate/issues/8 open -a Google\ Chrome --args --disable-web-secu ...
- python 的eval函数
python中的eval()函数是用来计算所有数学的代数计算式,这样可以很快得到复杂代数式的结果. 例如:383660347*375705824-1796136991-1726898699*18994 ...
- luogu P1736 创意吃鱼法
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #i ...
- QS Network ZOJ - 1586 板子题
#include<iostream> #include<algorithm> using namespace std; ; struct edge{ int a,b; doub ...
- html css二级导航栏
二级导航栏制作: 1.将一级导航栏去除列表样式(list-style:none),并给予浮动,使其横向排列(float:left) 2.给每个li中添加一个<a></a>标签, ...
- springboot里面的缓存注解
https://blog.csdn.net/u012240455/article/details/80844361 https://lfvepclr.gitbooks.io/spring-framew ...
- 518-零钱兑换 II(完全背包-求方案总数)
518-零钱兑换 II(完全背包-求方案总数) 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币有无限个. 示例 1: 输入: amount = 5, c ...
- spring整合websocket,如何在服务端依赖注入service
1.在pom.xml文件中添加jar包: <properties> <spring.version>4.0.5.RELEASE</spring.version> & ...
- 2020 Plan
1. English IELTS 7.0 IELTS word 3271, 10 words diff 2 Grammar framework IELTS writing Friends foll ...