在 C++ 标准库中,std::greater
是一个函数对象,用于确定两个值的顺序。std::greater
是一个模板类,可以通过包含 <functional>
头文件来使用。
std::greater
提供了一个二元函数对象,按照严格的大于运算符来确定元素的顺序。例如,如果用于比较的两个元素 a
和 b
,std::greater()(a, b)
将返回 true
,如果 a > b
,否则返回 false
。
以下是 std::greater
的简单示例用法:
#include <iostream>
#include <functional>
int main() {
std::greater<int> greater_than;
int a = 10;
int b = 5;
if(greater_than(a, b)) {
std::cout << "a is greater than b" << std::endl;
} else {
std::cout << "b is greater than a" << std::endl;
}
return 0;
}
在这个示例中,我们创建了一个 std::greater<int>
实例 greater_than
,然后使用它来比较两个整数 a
和 b
的大小。
总的来说,std::greater
是 C++ 标准库中用于按照大于运算符进行比较的函数对象,可用于各种算法中,如排序等。