C++函数对象-运算符函数对象 - 旧式绑定器与适配器 - 与适配器兼容的二元函数基类 (std::binary_function)

本文详细解释了C++中函数对象的概念,包括运算符函数的使用,以及旧式绑定器和适配器的背景。特别提到了std::binary_function的基础和它在C++11及后续版本中的弃用情况。并通过一个实例展示了如何使用这些概念,如std::transform配合std::not2进行操作。
摘要由CSDN通过智能技术生成

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

运算符函数对象


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

旧式绑定器与适配器


早期提供功能支持的几个工具在 C++11 中弃用,并于 C++17 中移除(旧否定器于 C++17 中弃用并于 C++20 中移除):

基类

与适配器兼容的二元函数基类 

std::binary_function
template<

    class Arg1,
    class Arg2,
    class Result

> struct binary_function;
(C++11 中弃用)
(C++17 中移除)

binary_function 是用于创建拥有二个参数的函数对象的基类。

binary_function 不定义 operator() ;它期待导出类将定义此运算符。 binary_function 只提供三个类型—— first_argument_typesecond_argument_typeresult_type ——为模板形参所定义。

一些标准库函数适配器,如 std::not2 要求其适配的函数对象必须定义这些类型; std::not2 要求要适配的函数对象必须拥有二个名为 first_argument_typesecond_argument_type 的类型。从 binary_function 导出接受二个参数的函数对象是令它们与那些适配器兼容的简便方式。

binary_function 于 C++11 弃用并于 C++17 移除。

成员类型

类型定义
first_argument_typeArg1
second_argument_typeArg2
result_typeResult

调用示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <iterator>

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

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

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

struct same : std::binary_function<Cell, Cell, bool>
{
    bool operator()(const Cell &cell1, const Cell &cell2) const
    {
        return cell1.x == cell2.x && cell1.y == cell2.y;
    }
};

int main()
{
    std::vector<Cell> vector1;
    for (int index = 1024 - 5; index < 1024 + 6; ++index)
    {
        vector1.push_back(Cell{index, index});
    }
    std::vector<Cell> vector2(vector1.rbegin(), vector1.rend());
    std::vector<bool> vector3(vector1.size(), false);

    std::transform(vector1.begin(), vector1.end(), vector2.begin(),
                   vector3.begin(), std::not2(same()));

    std::cout << std::boolalpha;
    for (size_t index = 0; index < vector1.size(); index++)
    {
        std::cout << vector1.at(index) << " " << vector2.at(index)
                  << " " << vector3.at(index) << std::endl;
    }

    return 0;
}

输出

{1019,1019} {1029,1029} true
{1020,1020} {1028,1028} true
{1021,1021} {1027,1027} true
{1022,1022} {1026,1026} true
{1023,1023} {1025,1025} true
{1024,1024} {1024,1024} false
{1025,1025} {1023,1023} true
{1026,1026} {1022,1022} true
{1027,1027} {1021,1021} true
{1028,1028} {1020,1020} true
{1029,1029} {1019,1019} true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值