c++ - std::is_sorted 和 strictly less 比较?

标签 c++ sorting c++11 comparator stl-algorithm

我不太明白std::is_sorted算法及其默认行为。如果我们查看 cppreference , 它表示默认情况下 std::is_sorted使用 <运算符(operator)。相反,我发现使用 <=会很自然。但我的问题是,对于以下数字列表:

1 2 3 3 4 5

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

编辑:它似乎比我想象的更糟糕,因为通过了 std::less_equal<int>在这种情况下将返回 false...当我传递比较器函数时应用的条件是什么?

最佳答案

根据 25.4/5:

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)将返回 false对于所有 n , 而 std::less_equal将返回 true对于案例 3 3 .

关于c++ - std::is_sorted 和 strictly less 比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17769114/

相关文章:

c++ - 修改程序以加密大写和小写输入

c++ - 这两个函数签名有什么区别?

c++ - 如何复制一个非平凡的 C++ union ?

c++ - 在 C++11 中获取当前时间的最快方法是什么?

c++ - 返回符合C++标准

c++ - 将行附加到 Armadillo .mat 文件

php - PHP 和 MySQL 中的计数然后排序函数

php - 按键值对 PHP 中的 JSON 对象进行排序

arrays - 对作为哈希值的数组的数组进行排序

c++ - MFC 应用程序启动后立即崩溃