std :: is_sorted和严格较少的比较? [英] std::is_sorted and strictly less comparison?
问题描述
我不太了解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 iteratori
pointing to the sequence and any non-negative integern
such thati + 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屋!