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

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

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

运算符函数对象

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

比较

实现 x == y 的函数对象

std::equal_to

template< class T >
struct equal_to;

(C++14 前)

template< class T = void >
struct equal_to;

(C++14 起)

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

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

特化

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

equal_to<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::equal_to::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;
    }

    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    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;
    }
};

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::equal_to<int*>()(1023, 1024):        "
              << std::equal_to<int*>()(ptr, nullptr) << std::endl;

    std::cout << "std::equal_to<char>()(50, 2):             "
              << std::equal_to<char>()(50, 2) << std::endl;
    std::cout << "std::equal_to<char>()('a', 97):           "
              << std::equal_to<char>()('a', 97) << std::endl;
    std::cout << "std::equal_to<int>()(1023, 1024):         "
              << std::equal_to<int>()(1023, 1024) << std::endl;
    std::cout << "std::equal_to<long>()(1023, 1024):        "
              << std::equal_to<long>()(1023, 1024) << std::endl;
    std::cout << "std::equal_to<long long>()(1023, 1024):   "
              << std::equal_to<long long>()(1023, 1024) << std::endl;

    std::cout << "std::equal_to<uint8_t>()(1023, 1024):     "
              << std::equal_to<uint8_t>()(8, 32) << std::endl;
    std::cout << "std::equal_to<uint16_t>()(123, 456):      "
              << std::equal_to<uint16_t>()(123, 456) << std::endl;
    std::cout << "std::equal_to<uint32_t>()(101, 202):      "
              << std::equal_to<uint32_t>()(101, 202) << std::endl;
    std::cout << "std::equal_to<uint64_t>()(10230, 10240):  "
              << std::equal_to<uint64_t>()(10230, 10240) << std::endl;

    std::cout << "std::equal_to<int8_t>()(1023, 1024):      "
              << std::equal_to<int8_t>()(8, 32) << std::endl;
    std::cout << "std::equal_to<int16_t>()(123, 456):       "
              << std::equal_to<int16_t>()(123, 456) << std::endl;
    std::cout << "std::equal_to<int32_t>()(101, 202):       "
              << std::equal_to<int32_t>()(101, 202) << std::endl;
    std::cout << "std::equal_to<int64_t>()(10230, 10240):   "
              << std::equal_to<int64_t>()(10230, 10240) << std::endl;

    std::cout << "std::equal_to<double>()(3.14, 3.14):      "
              << std::equal_to<double>()(3.14, 3.14) << std::endl;
    std::cout << "std::equal_to<float>()(3.14, 3.14):       "
              << std::equal_to<float>()(3.14, 3.14) << std::endl;
    std::cout << "std::equal_to<float>()(3, 3):             "
              << std::equal_to<float>()(3, 3) << std::endl;
    std::cout << "std::equal_to<float>()(3.56, 3.14):       "
              << std::equal_to<float>()(3.56, 3.14) << std::endl;
    std::cout << "std::equal_to<int>()(3.14, 3.14):         "
              << std::equal_to<int>()(3.34, 3.34) << std::endl;

    std::cout << "std::equal_to<Cell>()(Cell{101, 101}, Cell{202, 202}):       "
              << std::equal_to<Cell>()(Cell{101, 101}, Cell{202, 202}) << std::endl;

    std::cout << "std::equal_to<std::string>()(\"I am a \", \"handsome programmer\"):"
              << std::equal_to<std::string>()("I am a ", "handsome programmer") << std::endl;
    return 0;
}

输出

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


http://www.kler.cn/a/227076.html

相关文章:

  • containerd中文翻译系列(二) 从源码构建CONTAINERD
  • 032 数组
  • 揭秘远程控制APP的便捷之美!
  • Day 1. 学习linux高级编程之Shell命令和IO
  • EasyCVR视频融合平台如何助力执法记录仪高效使用
  • python25-Python的运算符之索引运算符
  • 【智能家居入门2】(MQTT协议、微信小程序、STM32、ONENET云平台)
  • OpenFeign学习使用
  • React16源码: React中event事件对象的创建过程源码实现
  • 【C语言】大小写字母的相互转化:多种方法解析及原理说明
  • 2V2无人机红蓝对抗仿真
  • FPGA高端项目:IMX327 MIPI 视频解码 USB3.0 UVC 输出,提供FPGA开发板+工程源码+技术支持
  • ChatGPT实战100例 - (13) 写一个属于自己的 ChatGPT 新版 WebUI
  • centos7如何删除整个文件夹里面的文件
  • 【漏洞复现】likeshop开源免费商用电商系统存在任意文件上传漏洞CVE-2024-0352
  • Excel技能——使用条件格式保护数据
  • app逆向-frida-rpc详解
  • eCos flash模拟EEPROM实现NV系统
  • 如何基于文档的内容实现 AI 对话功能,以 Documate 为例
  • Debezium发布历史106