news 2026/5/13 7:26:19

std::greater结构体用在sort和lower_bound

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
std::greater结构体用在sort和lower_bound

https://cn.bing.com/search?pglt=417&q=greater%3Cstring%3E

std::sort(numbers, numbers + 5, std::greater<int>());,std::greater{}也可以

#if _LIBCPP_STD_VER >= 14 template <class _Tp = void> #else template <class _Tp> #endif struct _LIBCPP_TEMPLATE_VIS greater : __binary_function<_Tp, _Tp, bool> { typedef bool __result_type; // used by valarray _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; } }; _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(greater);

std::greater in C++

The std::greater is a function object class template in C++ that performs a greater-than comparison between two values. It is defined in the header and is part of the C++ Standard Library. This function object returns true if the first argument is greater than the second argument, and false otherwise.

Definition and Usage

The std::greater template is defined as follows:

#include <functional> template <class T> struct greater { bool operator()(const T& x, const T& y) const { return x > y; } };

This template can be used to create objects that can be called with the same syntax as a function call. The operator() member function performs the comparison using the > operator.

Example

Here is an example of how to use std::greater with the std::sort algorithm to sort an array in descending order:

#include <iostream> #include <functional> #include <algorithm> int main() { int numbers[] = {20, 40, 50, 10, 30}; std::sort(numbers, numbers + 5, std::greater<int>()); for (int i = 0; i < 5; i++) std::cout << numbers[i] << ' '; std::cout << '\n'; return 0; }

Output: 50 40 30 20 10

In this example, std::greater() is used as the comparison function for std::sort, which sorts the array in descending order.

Practical Applications

The std::greater function object is useful in various standard algorithms such as std::sort, std::merge, and std::lower_bound where a greater-than comparison is needed. It provides a convenient and readable way to specify custom comparison logic without having to define separate comparison functions.

Important Considerations

Template Parameter: The type T must support the > operator for std::greater to work correctly.

Member Types: The std::greater template defines member types such as first_argument_type, second_argument_type, and result_type, which are deprecated in C++17 and removed in C++20.

By using std::greater, you can easily implement greater-than comparisons in your C++ programs, making your code more concise and expressive.

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/13 13:28:50

【拯救HMI】构建您的HMI组件库:提升设计一致性与开发效率的终极法则

如何确保不同设计师、不同项目之间的HMI保持统一风格&#xff1f;答案在于构建和维护一个企业级的、可复用的HMI组件库。什么是HMI组件库&#xff1f;它是一套预制的、标准化的UI元素集合&#xff0c;包括按钮、输入框、选择器、警报框、趋势图控件等&#xff0c;每个组件都包含…

作者头像 李华
网站建设 2026/5/13 13:29:20

大厂都这么干,没人管么?

昨天写闲鱼的文章引起了很多读者的讨论&#xff0c;看来不止我一个人觉得烦&#xff0c;很多人也都有同感。 其实我的观点很直接&#xff0c;不打扰&#xff0c;也是产品体验的一部分。 我在评论区区还看到这么一条留言&#xff0c;觉得有必要单独拿出来聊一聊&#xff0c;因为…

作者头像 李华
网站建设 2026/5/13 13:28:59

[Windows] 360极速浏览器v23.1.1137.64绿简版

[Windows] 360极速浏览器v23.1.1137.64绿简版 链接&#xff1a;https://pan.xunlei.com/s/VOgv2MyZyLbh8szAIb79dT56A1?pwd28cb# 鉴于还有好多人或公司仍然在使用360的浏览器&#xff0c;特制作此绿色版&#xff0c;方便携带使用。 感谢 360团队 360极速浏览器&#xff0c…

作者头像 李华