c++ - std::greater{} 和 std::greater<int>() 有什么区别?

标签 c++ stl

有些人写

std::nth_element(v.begin(), v.begin()+1, v.end(), std::greater{});
还有一些是这样写的
std::nth_element(v.begin(), v.begin()+1, v.end(), std::greater<int>());
std::greater{}之间有什么区别和 std::greater<int>() ?

最佳答案

这是 C++14 的新功能,这导致了两个完全不同的类。
在 C++14 中,std::greater为其模板参数获取默认值:void .
您最终会得到 std::greater<void>std::greater<int> .std::greater<void>so-called "transparent" comparator 的特化推导出其参数,请参阅其引用以获取更多信息。

关于c++ - std::greater{} 和 std::greater<int>() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68984321/

相关文章:

c++ - 在 map 中使用前类的前向声明错误

c++ - 在不重新分配内存的情况下将 vector 拆分为 2 个子范围

C++ Win32 - 跟踪私有(private)聊天的 ListView 项目

c++ - 从类中的对象调用类中函数的最佳方法

c++ - 如何找到数组的长度?

c++ - std 或 boost 中是否有类似循环预付的东西?

c++ - 是否有任何 std::set 实现不使用红黑树?

c++ - 将交互式控制台附加到嵌入式 python 脚本

c++ - 简单的 Windows C++ 不可见

c++ - 与三值比较函数相比,仅使用小于运算符进行排序