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

相关文章

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window