C++函数对象-运算符函数对象-算术运算-实现 x + y 的函数对象(std::plus)

本文详细解释了C++中如何使用std::plus函数对象进行各种类型的算术运算,包括模板特化和类的自定义运算符,以及在实际代码中的调用示例。
摘要由CSDN通过智能技术生成

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

运算符函数对象

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

算术运算

实现 x + y 的函数对象

std::plus

template< class T >
struct plus;

(C++14 前)

template< class T = void >
struct plus;

(C++14 起)

进行加法的函数对象。等效地调用 T 类型的二个实例上的 operator+ 。

特化

标准库在不指定 T 时提供 std::plus 的特化,这令参数类型和返回类型需要被推导。

plus<void>

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

成员类型

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

成员函数

operator()

返回两个实参的和
(公开成员函数)

std::plus::operator()

T operator()( const T& lhs, const T& rhs ) const;

(C++14 前)

constexpr T operator()( const T& lhs, const T& rhs ) const;

(C++14 起)

返回 lhsrhs 的和。

参数

lhs, rhs-要求和的值

返回值

lhs + rhs 的结果。

异常

(无)

 

可能的实现

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

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::cout << "std::plus<char>()(50, 2):             "
              << std::plus<char>()(50, 2) << std::endl;
    std::cout << "std::plus<int>()(1023, 1024):         "
              << std::plus<int>()(1023, 1024) << std::endl;
    std::cout << "std::plus<long>()(1023, 1024):        "
              << std::plus<long>()(1023, 1024) << std::endl;
    std::cout << "std::plus<long long>()(1023, 1024):   "
              << std::plus<long long>()(1023, 1024) << std::endl;

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

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

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

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

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

输出

std::plus<char>()(50, 2):             4
std::plus<int>()(1023, 1024):         2047
std::plus<long>()(1023, 1024):        2047
std::plus<long long>()(1023, 1024):   2047
std::plus<uint8_t>()(1023, 1024):     (
std::plus<uint16_t>()(123, 456):      579
std::plus<uint32_t>()(101, 202):      303
std::plus<uint64_t>()(10230, 10240):  20470
std::plus<int8_t>()(1023, 1024):      (
std::plus<int16_t>()(123, 456):       579
std::plus<int32_t>()(101, 202):       303
std::plus<int64_t>()(10230, 10240):   20470
std::plus<double>()(3.14, 3.14):      6.28
std::plus<float>()(3.14, 3.14):       6.28
std::plus<float>()(3, 3):             6
std::plus<int>()(3.14, 3.14):         6
std::plus<Cell>()(Cell{101, 101}, Cell{202, 202}):       {303,303}
std::plus<std::string>()("I am a ", "handsome programmer"):I am a handsome programmer

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值