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

相关文章

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT