std :: is_sorted和严格较少的比较? [英] std::is_sorted and strictly less comparison?

查看:125
本文介绍了std :: is_sorted和严格较少的比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解std::is_sorted算法及其默认行为.如果我们查看 cppreference ,则表示默认情况下std::is_sorted使用<运算符.取而代之的是,我发现使用<=是很自然的.但是我的问题是,对于以下数字列表:

I do not understand well the std::is_sorted algorithm and its default behaviour. If we look to cppreference, it says that by default std::is_sorted uses the < operator. Instead of that, I find that using <= would be natural. But my problem is that for the following list of numbers :

1 2 3 3 4 5

它将返回true,即使3 < 3应该为false.那怎么可能?

it will return true, even if 3 < 3 should be false. How is that possible ?

它似乎比我想的要糟,因为在这种情况下传递std::less_equal<int>会返回false ...当我传递比较器函数时应用的条件是什么?

its seems to be worse than what I thought, because passing std::less_equal<int> will return false in that case... What is the condition applied when I pass a comparator function?

推荐答案

每25.4/5:

根据比较器comp对序列进行排序(如果有的话) 迭代器i指向序列和任何非负整数n 这样i + n是一个有效的迭代器,它指向的元素 顺序 comp(*(i + n), *i) == false .

A sequence is sorted with respect to a comparator comp if for any iterator i pointing to the sequence and any non-negative integer n such that i + n is a valid iterator pointing to an element of the sequence, comp(*(i + n), *i) == false.

所以,对于

1 2 3 3 4 5

std::less<int>()(*(i + n), *i)将为所有n返回false,而std::less_equal将为情况3 3返回true.

std::less<int>()(*(i + n), *i) will return false for all n, while std::less_equal will return true for case 3 3.

这篇关于std :: is_sorted和严格较少的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆