我知道这是一件小事,但我想准确说明我对 std::sort()
的理解.
给定函数模板
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
可以阅读以下内容 here (我的重点):
comp - comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second.
但是在下面的示例中,我们可以看到 std::greater<int>()
都不是也不是 lambda 表达式 [](int a, int b) { return b < a; }
满足比较函数对象的这个要求。
最佳答案
初读时有些困惑,但如果您查看 Type requirements
部分,它会说:
Compare must meet the requirements of Compare.
指向C++ concepts: Compare它说(强调我的):
The return value of the function call operation applied to an object of type Compare, when contextually converted to bool, yields true if the first argument of the call appears before the second in the strict weak ordering relation induced by this Compare type, and false otherwise.
并继续建立 comp(a, b)
的要求,即它与以下属性建立严格的弱排序关系:
- 对于所有a,comp(a,a)==false
- 如果 comp(a,b)==true 那么 comp(b,a)==false
- 如果 comp(a,b)==true 且 comp(b,c)==true 那么 comp(a,c)==true
关于c++ - 这是文档中的一个小错误还是我遗漏了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21536342/