微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – std :: is_sorted和严格的比较?

我不太了解std :: is_sorted算法及其认行为.如果我们看到 cppreference,它说认情况下std :: is_sorted使用<操作符.而不是这样,我发现使用< =将是自然的.但是我的问题是下列数字列表:
1 2 3 3 4 5

它将返回true,即使3< 3应该是假的怎么可能? 编辑:它似乎比我想象的更糟糕,因为传递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)将为所有n返回false,而std :: less_equal将为case 3 3返回true.

原文地址:https://www.jb51.cc/c/113984.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐