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

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

运算符函数对象

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

比较

实现 x <= y 的函数对象

std::less_equal

template< class T >
struct less_equal;

(C++14 前)

template< class T = void >
struct less_equal;

(C++14 起)

进行比较的函数对象。调用类型 T 上的 operator<= ,除非特化。

特化

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

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

(C++14 起)

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

less_equal<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::less_equal::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;}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;}}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::less_equal<int*>()(1023, 1024):        "<< std::less_equal<int*>()(ptr, nullptr) << std::endl;std::cout << "std::less_equal<char>()(50, 2):             "<< std::less_equal<char>()(50, 2) << std::endl;std::cout << "std::less_equal<char>()('a', 97):           "<< std::less_equal<char>()('a', 97) << std::endl;std::cout << "std::less_equal<int>()(1023, 1024):         "<< std::less_equal<int>()(1023, 1024) << std::endl;std::cout << "std::less_equal<long>()(1023, 1024):        "<< std::less_equal<long>()(1023, 1024) << std::endl;std::cout << "std::less_equal<long long>()(1023, 1024):   "<< std::less_equal<long long>()(1023, 1024) << std::endl;std::cout << "std::less_equal<uint8_t>()(1023, 1024):     "<< std::less_equal<uint8_t>()(8, 32) << std::endl;std::cout << "std::less_equal<uint16_t>()(123, 456):      "<< std::less_equal<uint16_t>()(123, 456) << std::endl;std::cout << "std::less_equal<uint32_t>()(101, 202):      "<< std::less_equal<uint32_t>()(101, 202) << std::endl;std::cout << "std::less_equal<uint64_t>()(10230, 10240):  "<< std::less_equal<uint64_t>()(10230, 10240) << std::endl;std::cout << "std::less_equal<int8_t>()(1023, 1024):      "<< std::less_equal<int8_t>()(8, 32) << std::endl;std::cout << "std::less_equal<int16_t>()(123, 456):       "<< std::less_equal<int16_t>()(123, 456) << std::endl;std::cout << "std::less_equal<int32_t>()(101, 202):       "<< std::less_equal<int32_t>()(101, 202) << std::endl;std::cout << "std::less_equal<int64_t>()(10230, 10240):   "<< std::less_equal<int64_t>()(10230, 10240) << std::endl;std::cout << "std::less_equal<double>()(3.14, 3.14):      "<< std::less_equal<double>()(3.14, 3.14) << std::endl;std::cout << "std::less_equal<float>()(3.14, 3.14):       "<< std::less_equal<float>()(3.14, 3.14) << std::endl;std::cout << "std::less_equal<float>()(3, 3):             "<< std::less_equal<float>()(3, 3) << std::endl;std::cout << "std::less_equal<float>()(3.56, 3.14):       "<< std::less_equal<float>()(3.56, 3.14) << std::endl;std::cout << "std::less_equal<int>()(3.14, 3.14):         "<< std::less_equal<int>()(3.34, 3.34) << std::endl;std::cout << "std::less_equal<Cell>()(Cell{101, 101}, Cell{202, 202}):       "<< std::less_equal<Cell>()(Cell{101, 101}, Cell{202, 202}) << std::endl;std::cout << "std::less_equal<std::string>()(\"I am a \", \"handsome programmer\"):"<< std::less_equal<std::string>()("I am a ", "handsome programmer") << std::endl;return 0;
}

输出

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/675905.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

K8S系列文章之 [使用 Alpine 搭建 k3s]

官方文档&#xff1a;K3s - 轻量级 Kubernetes | K3s 官方描述&#xff0c;可运行在 systemd 或者 openrc 环境上&#xff0c;那就往精简方向走&#xff0c;使用 alpine 做系统。与 RHEL、Debian 的区别&#xff0c;主要在防火墙侧&#xff1b;其他基础配置需求类似&#xff0…

带你快速入门js高级-基础

1.作用域 全局 scriptxx.js 局部 函数作用域{} 块作用域 const let 2.闭包 函数外有权访问函数内的变量, 闭包可以延长变量生命周期 function 函数名 () {return function () {// 这里的变量不会立刻释放} }3.垃圾回收 不在使用(引用的变量), 防止占用内存&#xff0c;需要…

[每周一更]-(第86期):PostgreSQL入门学习和对比MySQL

入门学习PostgreSQL可以遵循以下步骤&#xff1a; 安装 PostgreSQL&#xff1a; 首先&#xff0c;你需要在你的计算机上安装 PostgreSQL。你可以从 PostgreSQL 官方网站 下载适合你操作系统的安装包&#xff0c;并按照官方文档的指导进行安装。 学习 SQL&#xff1a; PostgreS…

C#系列-简介(1)

一&#xff0c;C#简介 C#&#xff08;读作“C Sharp”&#xff09;是一种由微软公司开发的、运行于.NET Framework和.NET Core&#xff08;现在统称为.NET&#xff09;之上的高级编程语言。C#结合了C的强大功能和Java的易用性&#xff0c;旨在成为一种“优雅且安全”的语言&am…

【大厂AI课学习笔记】【1.5 AI技术领域】(7)图像分割

今天学习到了图像分割。 这是我学习笔记的脑图。 图像分割&#xff0c;Image Segmentation&#xff0c;就是将数字图像分割为若干个图像子区域&#xff08;像素的集合&#xff0c;也被称为超像素&#xff09;&#xff0c;改变图像的表达方式&#xff0c;以更容易理解和分析。 …

〔Part1〕YOLOv5:原理+源码分析(配置文件、网络模块、损失函数、跨网格匹配策略)

1. 前置知识 1.1 YOLO 算法的基本思想 首先通过特征提取网络对输入图像提取特征&#xff0c;得到一定大小的特征图&#xff0c;比如 13x13&#xff08;相当于416x416 图片大小&#xff09;&#xff0c;然后将输入图像分成 13x13 个 grid cells&#xff1a; YOLOv3/v4&#xf…

leetcode:17.电话号码的字母组合

题意和解题思路&#xff1a; 数字和字母的一一对应采用二维数组或者map映射。 这里我采用数组进行存储进而实现一一对应。由于我们无法知道for循环嵌套几层&#xff0c;因为这个是由于输入来确定的&#xff0c;所以我们可以用回溯算法中的递归来进行实现。 树形结构&#xff…

2024.2.8日总结(小程序开发5)

对上拉触底事件进行节流处理 在data中定义isloading节流阀 false表示当前没有进行任何数据请求true表示当前正在进行数据请求 在getColors()方法中修改isloading节流阀的值 在刚调用getColors时将节流阀设置true在网络请求的complete回调函数中&#xff0c;将节流阀重置为f…

C语言:月份缩写

题目描述 从一月份到十二月的英文全称依次是&#xff1a;“January”,“February”,“March”,“April”,“May”,“June”,“July”,“August”,“September”,“October”,“November”,“December” 对应的缩写依次是&#xff1a;“Jan.”,“Feb.”,“Mar.”,“Apr.”,“Ma…

git版本回退。git reset参数详解,特殊提交情形下的git push操作(CR等常见场景),git reflog和git log的详解。

切换分支可以使用 git checkout <> 或者git switch ... 创建分支可以使用 git checkout -b <. ...> 或 git branch <...> git checkout <...> git reset --hrad HEAD^ -- 今日份chatgpt git reset --hard HEAD^ 的含义如下&#xff1a; git reset …

Kubernetes命令备忘单

Kubernetes 版本&#xff1a; v1.28 https://v1-28.docs.kubernetes.io/zh-cn/docs/reference/kubectl/cheatsheet/ 这些指令适用于 Kubernetes v1.28。要检查版本&#xff0c;请使用 kubectl version 命令。 本页列举常用的 kubectl 命令和参数。 Kubectl 自动补全 BASH …

已解决org.springframework.aop.AopInvocationException异常的正确解决方法,亲测有效!!!

已解决org.springframework.aop.AopInvocationException异常的正确解决方法&#xff0c;亲测有效&#xff01;&#xff01;&#xff01; 文章目录 问题分析 报错原因 解决思路 解决方法 总结 问题分析 org.springframework.aop.AopInvocationException通常发生在Spring的…

利用知识图谱构建医疗问答

1、准备数据集 数据集下载地址&#xff1a;https://github.com/wangle1218/QASystemOnMedicalKG/blob/master/data/medical.json 2、导入相关包 from py2neo import Graph,Node,Relationship # 在cmd中&#xff0c;输入neo4j.bat console并回车 import pandas as pd3、连接N…

C#系列-多线程(4)

在C#中&#xff0c;多线程编程主要涉及使用System.Threading命名空间下的类和接口来创建和管理线程。以下是一些C#多线程编程的基本用法和示例&#xff1a; 1. 使用Thread类创建线程 csharp代码 using System; using System.Threading; class Program { static void …

java大数据hadoop2.9.2 Flume安装操作

1、flume安装 &#xff08;1&#xff09;解压缩 tar -xzvf apache-flume-1.9.0-bin.tar.gz rm -rf apache-flume-1.9.0-bin.tar.gz mv ./apache-flume-1.9.0-bin/ /usr/local/flume &#xff08;2&#xff09;配置 cd /usr/local/flume/conf cp ./flume-env.sh.template…

14.Swift函数

Swift 函数 在 Swift 中&#xff0c;函数是一种用于执行特定任务或计算特定值的独立代码块。函数可以接受参数并返回一个值&#xff0c;也可以没有参数或返回值。以下是 Swift 中常用的函数操作&#xff1a; 1. 定义函数 可以使用 func 关键字定义函数&#xff0c;指定函数名…

【web前端开发】HTML及CSS简单页面布局练习

案例一 网页课程 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content"widthdevice-wi…

Android 识别车牌信息

打开我们心爱的Android Studio 导入需要的资源 gradle //开源车牌识别安卓SDK库implementation("com.github.HyperInspire:hyperlpr3-android-sdk:1.0.3")button.setOnClickListener(v -> {Log.d("Test", "");try (InputStream file getAs…

「递归算法」:子集(两种解法)

一、题目 给你一个整数数组 nums &#xff0c;数组中的元素 互不相同 。返回该数组所有可能的子集&#xff08;幂集&#xff09;。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3] 输出&#xff1a;[[],[1],[2]…

问题 | 开源软件的影响力

github&#xff1a;https://github.com/MichaelBeechan CSDN&#xff1a;https://blog.csdn.net/u011344545 开源软件的影响力 降低成本和提高效率&#xff1a;开源软件一般是免费提供的&#xff0c;企业和个人无需支付许可费用&#xff0c;从而降低了软件开发和使用的成本。此…