C++函数对象-运算符函数对象 - 旧式绑定器与适配器 - 构造定制的 std::unary_negate 对象 (std::not1)

本文详细介绍了C++中如何使用函数对象、运算符函数以及旧式绑定器和适配器,特别是std::unary_negate的用法,展示了如何通过`std::not1`创建一元谓词的补。还给出了C++11时代以lambda表达式替代旧适配器的方法。
摘要由CSDN通过智能技术生成

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

运算符函数对象


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

旧式绑定器与适配器


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

函数适配器

构造定制的 std::unary_negate 对象

std::not1

template< class Predicate >
std::unary_negate<Predicate> not1(const Predicate& pred);

(C++14 前)

template< class Predicate >
constexpr std::unary_negate<Predicate> not1(const Predicate& pred);

(C++14 起)
(C++17 中弃用)
(C++20 中移除)

not1 是用于创建返回传递的一元谓词的补的函数对象。创建的函数对象类型为 std::unary_negate<Predicate> 。

一元谓词类型必须定义成员类型 argument_type ,它可转换为谓词的参数类型。从 std::ref 、 std::cref 、 std::negate 、 std::logical_not 、 std::mem_fn 、 std::function 、 std::hash 或调用另一 std::not1 获得的 unary_function 对象定义此类型,如同从弃用的 std::unary_function 导出的函数对象。

参数

pred-一元谓词

返回值

std::not1 返回以 pred 构造的 std::unary_negate<Predicate> 类型对象。

异常

无。

 

调用示例

#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 less_than_1024 : std::unary_function<Cell, bool>
{
    bool operator()(const Cell &cell) const
    {
        return cell < Cell{1024, 1024};
    }
};

int main()
{
    std::vector<Cell> vector;
    for (int index = 1024 - 3; index < 1024 + 3; ++index)
    {
        vector.push_back(Cell{index, index});
    }

    std::copy(vector.begin(), vector.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "std::not1(less_than_1024()):  ";
    std::cout << std::count_if(vector.begin(), vector.end(), std::not1(less_than_1024())) << std::endl;

    /* C++11 解法:
        // 用某方法转型到 std::function<bool (Cell)> ——即使以 lambda
        std::cout << std::count_if(vector.begin(), vector.end(),
            std::not1(std::function<bool (Cell)>([](Cell cell){ return cell < Cell{1024, 1024}; }))
        );
    */

    return 0;
}

输出

{1021,1021} {1022,1022} {1023,1023} {1024,1024} {1025,1025} {1026,1026}
std::not1(less_than_1024()):  3

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值