C++函数对象-运算符函数对象 - 逻辑运算 - 实现 !x 的函数对象 (std::logical_not)

2024-02-12 17:04

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

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

运算符函数对象

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

逻辑运算

实现 !x 的函数对象

std::logical_not

template< class T >
struct logical_not;

(C++14 前)

template< class T = void >
struct logical_not;

(C++14 起)

实现逻辑非(逻辑取反)的函数对象。等效地调用类型 T 的 operator! 。

特化

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

logical_not<void>

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

成员类型

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

成员函数

operator()

返回参数的逻辑非
(公开成员函数)

调用示例

#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){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 &cell){return !(cell.x && cell.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::logical_not<int*>()(ptr, nullptr):   "<< std::logical_not<int*>()(ptr) << std::endl;std::cout << "std::logical_not<char>()(50):             "<< std::logical_not<char>()(50) << std::endl;std::cout << "std::logical_not<char>()('a'):            "<< std::logical_not<char>()('a') << std::endl;std::cout << "std::logical_not<int>()(1023):            "<< std::logical_not<int>()(1023) << std::endl;std::cout << "std::logical_not<long>()(1023):           "<< std::logical_not<long>()(1023) << std::endl;std::cout << "std::logical_not<long long>()(1023):      "<< std::logical_not<long long>()(1023) << std::endl;std::cout << "std::logical_not<uint8_t>()(1023):        "<< std::logical_not<uint8_t>()(8) << std::endl;std::cout << "std::logical_not<uint16_t>()(123):        "<< std::logical_not<uint16_t>()(123) << std::endl;std::cout << "std::logical_not<uint32_t>()(101):        "<< std::logical_not<uint32_t>()(101) << std::endl;std::cout << "std::logical_not<uint64_t>()(10230):      "<< std::logical_not<uint64_t>()(10230) << std::endl;std::cout << "std::logical_not<int8_t>()(1023):         "<< std::logical_not<int8_t>()(8) << std::endl;std::cout << "std::logical_not<int16_t>()(123):         "<< std::logical_not<int16_t>()(123) << std::endl;std::cout << "std::logical_not<int32_t>()(101):         "<< std::logical_not<int32_t>()(101) << std::endl;std::cout << "std::logical_not<int64_t>()(10230):       "<< std::logical_not<int64_t>()(10230) << std::endl;std::cout << "std::logical_not<double>()(3.14):         "<< std::logical_not<double>()(3.14) << std::endl;std::cout << "std::logical_not<float>()(3.14):          "<< std::logical_not<float>()(3.14) << std::endl;std::cout << "std::logical_not<float>()(3):             "<< std::logical_not<float>()(3) << std::endl;std::cout << "std::logical_not<float>()(3.56):          "<< std::logical_not<float>()(3.56) << std::endl;std::cout << "std::logical_not<int>()(3.14):            "<< std::logical_not<int>()(3.34) << std::endl;std::cout << "std::logical_not<Cell>()(Cell{101, 101}): "<< std::logical_not<Cell>()(Cell{101, 101}) << std::endl;std::cout << "std::logical_not<Cell>()(Cell{0, 0}):     "<< std::logical_not<Cell>()(Cell{0, 0}) << std::endl;//编译失败
//    std::cout << "std::logical_not<std::string>()(\"I am a handsome programmer\"):"
//              << std::logical_not<std::string>()("I am a handsome programmer") << std::endl;return 0;
}

输出

std::logical_not<int*>()(ptr, nullptr):   true
std::logical_not<char>()(50):             false
std::logical_not<char>()('a'):            false
std::logical_not<int>()(1023):            false
std::logical_not<long>()(1023):           false
std::logical_not<long long>()(1023):      false
std::logical_not<uint8_t>()(1023):        false
std::logical_not<uint16_t>()(123):        false
std::logical_not<uint32_t>()(101):        false
std::logical_not<uint64_t>()(10230):      false
std::logical_not<int8_t>()(1023):         false
std::logical_not<int16_t>()(123):         false
std::logical_not<int32_t>()(101):         false
std::logical_not<int64_t>()(10230):       false
std::logical_not<double>()(3.14):         false
std::logical_not<float>()(3.14):          false
std::logical_not<float>()(3):             false
std::logical_not<float>()(3.56):          false
std::logical_not<int>()(3.14):            false
std::logical_not<Cell>()(Cell{101, 101}): false
std::logical_not<Cell>()(Cell{0, 0}):     true

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


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

相关文章

使用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

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

MyBatisX逆向工程的实现示例

《MyBatisX逆向工程的实现示例》本文主要介绍了MyBatisX逆向工程的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录逆向工程准备好数据库、表安装MyBATisX插件项目连接数据库引入依赖pom.XML生成实体类、

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

Java实现MinIO文件上传的加解密操作

《Java实现MinIO文件上传的加解密操作》在云存储场景中,数据安全是核心需求之一,MinIO作为高性能对象存储服务,支持通过客户端加密(CSE)在数据上传前完成加密,下面我们来看看如何通过Java... 目录一、背景与需求二、技术选型与原理1. 加密方案对比2. 核心算法选择三、完整代码实现1. 加密上