C++函数对象-运算符函数对象 - 比较 - 实现 x > y 的函数对象 (std::greater)

2024-02-06 09:52

本文主要是介绍C++函数对象-运算符函数对象 - 比较 - 实现 x > y 的函数对象 (std::greater),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。

运算符函数对象

C++ 针对常用的算术和逻辑运算定义了很多函数对象:

比较

实现 x > y 的函数对象

std::greater

template< class T >
struct greater;

(C++14 前)

template< class T = void >
struct greater;

(C++14 起)

实现比较的函数对象。调用类型 T 上的 operator> ,除非特化。

特化

std::greater 的特化为任何指针类型产生严格全序,即使内建的 operator> 不如此。严格全序在 std::less 、 std::greater 、 std::less_equal 和 std::greater_equal 对该指针类型的特化间一致,且亦与对应的内建运算符( <><=>= )所强加的部分顺序一致。

若特化 std::greater<void> 的函数调用运算符调用内建运算符比较指针,则它产生严格全序,即使内建的 operator> 不如此。此严格全序在特化 std::less<void>std::greater<void>std::less_equal<void>std::greater_equal<void> 间一致,亦与对应的内建运算符所强加的部分顺序一致。

(C++14 起)

标准库提供 std::greater 在不指定 T 时的特化,使得参数类型和返回类型留待推导。

greater<void>

实现 x > y 并推导参数和返回类型的函数对象
(类模板特化)
(C++14 起)

 成员类型

类型定义
result_type(C++17 中弃用)bool
first_argument_type(C++17 中弃用)T
second_argument_type(C++17 中弃用)T
(C++20 前)

 

成员函数

operator()

检查第一参数是否大于第二个
(公开成员函数)

 

std::greater::operator()

bool operator()( const T& lhs, const T& rhs ) const;

(C++14 前)

constexpr bool operator()( const T& lhs, const T& rhs ) const;

(C++14 起)

检查 lhs 是否大于 rhs

参数

lhs, rhs-要比较的值

返回值

若 lhs > rhs 则为 true ,否则为 false 。

异常

(无)

可能的实现

constexpr bool operator()(const T &lhs, const T &rhs) const 
{return lhs > rhs;
}

调用示例

#include <iostream>
#include <functional>struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell(const Cell &cell){x = cell.x;y = cell.y;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}Cell &operator+(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator+=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator*=(int n){x *= n;y *= n;return *this;}Cell &operator++(){x += 1;y += 1;return *this;}friend Cell operator +(const Cell &cell1, const Cell &cell2){Cell cell = cell1;cell += cell2;return cell;}friend Cell operator *(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};return cell;}friend Cell operator /(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x / cell2.x, cell1.y / cell2.y};return cell;}friend Cell operator %(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x % cell2.x, cell1.y % cell2.y};return cell;}friend bool operator ==(const Cell &cell1, const Cell &cell2){return cell1.x == cell2.x && cell1.y == cell2.y;}friend bool operator !=(const Cell &cell1, const Cell &cell2){return cell1.x != cell2.x && cell1.y != cell2.y;}friend bool operator <(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y < cell2.y;}else{return cell1.x < cell2.x;}}friend bool operator >(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y > cell2.y;}else{return cell1.x > cell2.x;}}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}int main()
{std::cout << std::boolalpha;int *ptr = nullptr;std::cout << "std::greater<int*>()(1023, 1024):        "<< std::greater<int*>()(ptr, nullptr) << std::endl;std::cout << "std::greater<char>()(50, 2):             "<< std::greater<char>()(50, 2) << std::endl;std::cout << "std::greater<char>()('a', 97):           "<< std::greater<char>()('a', 97) << std::endl;std::cout << "std::greater<int>()(1023, 1024):         "<< std::greater<int>()(1023, 1024) << std::endl;std::cout << "std::greater<long>()(1023, 1024):        "<< std::greater<long>()(1023, 1024) << std::endl;std::cout << "std::greater<long long>()(1023, 1024):   "<< std::greater<long long>()(1023, 1024) << std::endl;std::cout << "std::greater<uint8_t>()(1023, 1024):     "<< std::greater<uint8_t>()(8, 32) << std::endl;std::cout << "std::greater<uint16_t>()(123, 456):      "<< std::greater<uint16_t>()(123, 456) << std::endl;std::cout << "std::greater<uint32_t>()(101, 202):      "<< std::greater<uint32_t>()(101, 202) << std::endl;std::cout << "std::greater<uint64_t>()(10230, 10240):  "<< std::greater<uint64_t>()(10230, 10240) << std::endl;std::cout << "std::greater<int8_t>()(1023, 1024):      "<< std::greater<int8_t>()(8, 32) << std::endl;std::cout << "std::greater<int16_t>()(123, 456):       "<< std::greater<int16_t>()(123, 456) << std::endl;std::cout << "std::greater<int32_t>()(101, 202):       "<< std::greater<int32_t>()(101, 202) << std::endl;std::cout << "std::greater<int64_t>()(10230, 10240):   "<< std::greater<int64_t>()(10230, 10240) << std::endl;std::cout << "std::greater<double>()(3.14, 3.14):      "<< std::greater<double>()(3.14, 3.14) << std::endl;std::cout << "std::greater<float>()(3.14, 3.14):       "<< std::greater<float>()(3.14, 3.14) << std::endl;std::cout << "std::greater<float>()(3, 3):             "<< std::greater<float>()(3, 3) << std::endl;std::cout << "std::greater<float>()(3.56, 3.14):       "<< std::greater<float>()(3.56, 3.14) << std::endl;std::cout << "std::greater<int>()(3.14, 3.14):         "<< std::greater<int>()(3.34, 3.34) << std::endl;std::cout << "std::greater<Cell>()(Cell{101, 101}, Cell{202, 202}):       "<< std::greater<Cell>()(Cell{101, 101}, Cell{202, 202}) << std::endl;std::cout << "std::greater<std::string>()(\"I am a \", \"handsome programmer\"):"<< std::greater<std::string>()("I am a ", "handsome programmer") << std::endl;return 0;
}

输出

std::greater<int*>()(1023, 1024):        false
std::greater<char>()(50, 2):             true
std::greater<char>()('a', 97):           false
std::greater<int>()(1023, 1024):         false
std::greater<long>()(1023, 1024):        false
std::greater<long long>()(1023, 1024):   false
std::greater<uint8_t>()(1023, 1024):     false
std::greater<uint16_t>()(123, 456):      false
std::greater<uint32_t>()(101, 202):      false
std::greater<uint64_t>()(10230, 10240):  false
std::greater<int8_t>()(1023, 1024):      false
std::greater<int16_t>()(123, 456):       false
std::greater<int32_t>()(101, 202):       false
std::greater<int64_t>()(10230, 10240):   false
std::greater<double>()(3.14, 3.14):      false
std::greater<float>()(3.14, 3.14):       false
std::greater<float>()(3, 3):             false
std::greater<float>()(3.56, 3.14):       true
std::greater<int>()(3.14, 3.14):         false
std::greater<Cell>()(Cell{101, 101}, Cell{202, 202}):       false
std::greater<std::string>()("I am a ", "handsome programmer"):false

这篇关于C++函数对象-运算符函数对象 - 比较 - 实现 x > y 的函数对象 (std::greater)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683933

相关文章

利用Python实现可回滚方案的示例代码

《利用Python实现可回滚方案的示例代码》很多项目翻车不是因为不会做,而是走错了方向却没法回头,技术选型失败的风险我们都清楚,但真正能提前规划“回滚方案”的人不多,本文从实际项目出发,教你如何用Py... 目录描述题解答案(核心思路)题解代码分析第一步:抽象缓存接口第二步:实现两个版本第三步:根据 Fea

Go语言使用slices包轻松实现排序功能

《Go语言使用slices包轻松实现排序功能》在Go语言开发中,对数据进行排序是常见的需求,Go1.18版本引入的slices包提供了简洁高效的排序解决方案,支持内置类型和用户自定义类型的排序操作,本... 目录一、内置类型排序:字符串与整数的应用1. 字符串切片排序2. 整数切片排序二、检查切片排序状态:

python利用backoff实现异常自动重试详解

《python利用backoff实现异常自动重试详解》backoff是一个用于实现重试机制的Python库,通过指数退避或其他策略自动重试失败的操作,下面小编就来和大家详细讲讲如何利用backoff实... 目录1. backoff 库简介2. on_exception 装饰器的原理2.1 核心逻辑2.2

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

基于C#实现MQTT通信实战

《基于C#实现MQTT通信实战》MQTT消息队列遥测传输,在物联网领域应用的很广泛,它是基于Publish/Subscribe模式,具有简单易用,支持QoS,传输效率高的特点,下面我们就来看看C#实现... 目录1、连接主机2、订阅消息3、发布消息MQTT(Message Queueing Telemetr

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R