c++ - 这是文档中的一个小错误还是我遗漏了什么?

标签 c++ c++11 std functor

我知道这是一件小事,但我想准确说明我对 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/

相关文章:

c++ - 找不到 boost::async 到底做了什么

c++ - 关于将字符串传递给 win32 api 函数

c++ - 如何实现自动插入隐含占位符的 easy_bind() ? *带有成员指针*

c++11 - 在 Red Hat linux 中在 g++ 4.4.7 上编译 C++11

c++ - 转发引用 : returning T when given T&& and T& when given T&

c++ - 循环结构的迭代器

c++ - 初始化 vector 和 std

c++ - 为什么 std::vector::push_back 需要赋值运算符

C++ 将对象写入二进制文件

c++ - 单位球体 C++ 上 N 个相互排斥点的蒙特卡罗(可能是模拟退火?)方法