news 2026/7/4 11:38:38

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/7/1 20:32:13

购物狂欢频繁被攻击:网络安全的价值与必备技能

电商平台涌动着千万订单&#xff0c;支付网关处理着海量交易请求&#xff0c;用户账户里存储着个人信息和资金余额&#xff0c;企业服务器承载着核心业务数据和商业秘密…… 每逢“双十一”、“黑五”等购物狂欢季&#xff0c;或是重大活动期间&#xff0c;我们总能看到“某平…

作者头像 李华
网站建设 2026/7/3 20:39:02

LLM学习宝典:从理论基础到工程实践的完整路径_大模型入门学习教程(非常详细)看这一篇就够了!

文章是一份全面的大模型(LLM)学习指南&#xff0c;分为三个主要部分&#xff1a;基础理论(数学基础、Python、神经网络、NLP)、科学家视角(LLM架构、数据集构建、预训练、微调等)和工程师视角(运行LLM、向量存储、RAG、部署等)。文章提供了系统化的学习路径&#xff0c;包括成长…

作者头像 李华
网站建设 2026/7/3 20:59:10

通用音频系统全链路实战指南

目录 总场景&#xff1a;做一个「直播 / 会议 / 短视频 / AI 语音通用音频系统」 一、PCM / WAV / MP3 / AAC —— 在系统里的真实角色 &#x1f3a4; 现场输入 ✅ PCM&#xff08;系统内部的“通用语言”&#xff09; WAV 是什么&#xff1f; MP3 / AAC 是什么&#xff…

作者头像 李华
网站建设 2026/7/3 15:04:58

低代码资源合集

mksz824-真实高质量低代码商业项目&#xff0c;前端后端运维管理系统 文件大小: 27.0GB内容特色: 27GB全栈低代码商业项目源码与运维脚本适用人群: 想快速落地企业级系统的开发者核心价值: 拿来即用&#xff0c;省80%编码量&#xff0c;附上线部署手册下载链接: https://pan.q…

作者头像 李华