当前位置: 首页 > article >正文

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


http://www.kler.cn/news/234670.html

相关文章:

  • Java 集合、迭代器
  • 跟着cherno手搓游戏引擎【24】开启2D引擎前的项目总结(包括前置知识汇总)
  • 【大厂AI课学习笔记】【1.6 人工智能基础知识】(2)机器学习
  • 07-Java桥接模式 ( Bridge Pattern )
  • 网络学习:数据链路层VLAN原理和配置
  • tkinter-TinUI-xml实战(10)展示画廊
  • mac卸载被锁定的app
  • 《CSS 简易速速上手小册》第4章:视觉美学(2024 最新版)
  • Python for 循环
  • 常见性能优化策略
  • CVE-2012-2311 漏洞复现
  • 计算机网络(第六版)复习提纲29
  • spring boot 通过 application 切换cache使用的服务
  • React18原理: 再聊Fiber架构下的时间分片
  • 前端JavaScript篇之ajax、axios、fetch的区别
  • 【LeetCode每日一题】二维前缀和基本概念与案例
  • 剪辑思维大学习(Day5) - 剪辑时如何找到合适的音乐?!
  • #Z2322. 买保险
  • 【自然语言处理-工具篇】spaCy<2>--模型的使用
  • 请解释Java中的代理模式,分别介绍静态代理和动态代理
  • WindowsLinuxmeterepreter渗透命令回顾
  • windows 下安装gin
  • PKI - 借助Nginx实现_客户端使用CA根证书签发客户端证书
  • django中实现适配器模式
  • python基于flask的网上订餐系统769b9-django+vue
  • SNMP(简单网络管理协议)介绍
  • 【每日一题】04最小路径和 (DP3)
  • 【C语言】(20)动态内存分配
  • HiveQL——不借助任何外表,产生连续数值
  • Linux CentOS stream 9 alias