《21天学通C++》(第二十一章)理解函数对象

什么是函数对象?

函数对象是一种特殊类型的类 ,它重载 了函数调用操作符 operator(),使得类的实例可以像函数一样被调用。
什么是谓词?

谓词是指一个能够返回布尔值(true或false)的函数或函数对象

1.一元函数

一元函数是指接受单个参数的函数

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

// 定义一元函数对象,用于打印整数
struct Print {//定义一个结构体
    void operator()(int x) const {//重载operator()
        std::cout << x << " ";
    }
};

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    
    // 使用一元函数对象显示集合内容
    std::for_each(numbers.begin(), numbers.end(), Print());
    //开始迭代器,结束迭代器,操作函数(函数对象或Lambda表达式)
    //调用Print()
    
    std::cout << std::endl;
    system("pause");
    return 0;
}

2.一元谓词

一元谓词是一种特殊的一元函数,它接受单个参数并返回一个布尔值

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

// 定义一个一元谓词,用于判断是否为某个数字的整数
struct IsMultipleOf {//

    int Divisor;
    IsMultipleOf(int Divisor) : Divisor(Divisor) {}

    bool operator()(int number) const {//重载operator(),返回布尔值
        return ((number % Divisor) == 0);
    }
};

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 10};
    
    // 使用一元谓词 IsMultipleOf 来查找第一个是5的倍数的数字
    auto it = std::find_if(numbers.cbegin(), numbers.cend(), IsMultipleOf(5));
    
    if (it != numbers.cend()) {
        std::cout << "The  number is: " << *it << std::endl;
    } else {
        std::cout << "No number" << std::endl;
    }
    system("pause");
    return 0;
}

3.二元函数

一元函数是指接受两个参数的函数

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

// 定义一个二元函数,实现两个数相乘
struct AddFunctor {//定义一个结构体
    int operator()(int a, int b) const {//重载operator()
        return a * b;
    }
};

int main() {
    std::vector<int> numbers1 = {1, 2, 3, 4, 5};
    std::vector<int> numbers2 = {5, 4, 4, 2, 2};
    
    // 使用二元函数计算两个向量对应元素的乘积
    std::vector<int> result(numbers1.size());//定义一个新的vector用来存储结果

    std::transform(numbers1.begin(), numbers1.end(), 
                    numbers2.begin(), result.begin(), 
                    AddFunctor());//调用AddFunctor()

    // 显示结果
    for (int num : result) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    system("pause");
    return 0;
}

4.二元谓词

一元谓词是一种特殊的二元函数,它接受两个参数并返回一个布尔值

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

// 二元谓词,用于比较两个整数的大小
struct GreaterThan {
    bool operator()(int a, int b) const {
        return a > b;  // 返回 a 是否大于 b
    }
};

int main() {
    std::vector<int> numbers = {10, 20, 30, 40, 50};

    // 使用二元谓词对向量进行降序排序
    std::sort(numbers.begin(), numbers.end(), GreaterThan());

    // 输出排序后的向量
    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    system("pause");
    return 0;
}
相关推荐
Piar1231sdafa4 分钟前
基于yolo13-C3k2-RVB的洗手步骤识别与检测系统实现_1
人工智能·算法·目标跟踪
a程序小傲5 分钟前
中国邮政Java面试被问:Netty的FastThreadLocal优化原理
java·服务器·开发语言·面试·职场和发展·github·哈希算法
做科研的周师兄5 分钟前
【MATLAB 实战】|多波段栅格数据提取部分波段均值——批量处理(NoData 修正 + 地理信息保真)_后附完整代码
前端·算法·机器学习·matlab·均值算法·分类·数据挖掘
淦。。。。9 分钟前
题解:P14013 [POCamp 2023] 送钱 / The Generous Traveler
开发语言·c++·经验分享·学习·其他·娱乐·新浪微博
橙露14 分钟前
C#在视觉检测中的优势:工业智能化转型的利器
开发语言·c#·视觉检测
醇氧14 分钟前
java.lang.NumberFormatException: For input string: ““
java·开发语言·spring
利刃大大18 分钟前
【ES6】变量与常量 && 模板字符串 && 对象 && 解构赋值 && 箭头函数 && 数组 && 扩展运算符 && Promise/Await/Async
开发语言·前端·javascript·es6
天赐学c语言19 分钟前
1.18 - 滑动窗口最大值 && 子类的指针转换为父类的指针,指针的值是否会改变
数据结构·c++·算法·leecode
大猫会长24 分钟前
postgreSQL中,RLS的using与with check
开发语言·前端·javascript
老蒋每日coding36 分钟前
Python:数字时代的“万能钥匙”
开发语言·python