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

相关文章

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最