Day 29 C++ STL- 函数对象(Function Object)(仿函数)

函数对象概念

概念

  • 重载函数调用操作符的类,其对象常称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

函数对象是一种行为类似于函数的对象。它是一个类或结构体,重载了函数调用操作符 operator()。当使用函数对象时,可以像调用函数一样对其进行调用。

函数对象可以具有状态并在调用之间保持状态,这使得函数对象在某些情况下比普通函数更加灵活和功能强大。

本质

函数对象(仿函数)是一个 重载了函数调用操作符()的类,不是一个函数

函数对象使用

特点

  • 函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 仿函数写法非常灵活,可以作为参数进行传递

示例

#include <iostream>
#include <algorithm>
#include <vector>

// 函数对象类
class MultiplyBy {
public:
    MultiplyBy(int factor) : factor_(factor) {}

    int operator()(int x) const {
        return x * factor_;
    }

private:
    int factor_;
};

int main() {
    // 创建函数对象实例
    MultiplyBy multiplyByTwo(2);

    // 函数对象调用
    int result = multiplyByTwo(3);  // 结果为 6
    std::cout << "Result: " << result << std::endl;

    // 函数对象作为参数传递给算法
    std::vector<int> nums = {1, 2, 3, 4, 5};
    std::transform(nums.begin(), nums.end(), nums.begin(), MultiplyBy(3));

    // 输出结果:3 6 9 12 15
    for (int num : nums) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

上述示例中,我们定义了一个函数对象类 MultiplyBy,它接受一个整数参数,在函数调用操作符 operator() 中将传入的参数与元素相乘。我们使用函数对象实例 multiplyByTwo 进行函数调用,以及将函数对象 MultiplyBy(3) 作为参数传递给 std::transform() 算法,对容器 nums 中的元素进行乘法操作。


谓词——返回bool类型的仿函数

谓词概念

  • 返回bool类型的仿函数称为谓词
  • 如果operator()接受一个参数,那么叫做一元谓词
  • 如果operator()接受两个参数,那么叫做二元谓词

一元谓词——operator()参数只有一个的谓词

#include <vector>
#include <algorithm>

//1.一元谓词
struct GreaterFive{
	bool operator()(int val) {
		return val > 5;
	}
};

void test01() {

	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "没找到!" << endl;
	}
	else {
		cout << "找到:" << *it << endl;
	}
}

int main() {
	test01();
	system("pause");
	return 0;
}

二元谓词——operator()参数只有俩个的谓词

#include <vector>
#include <algorithm>
//二元谓词
class MyCompare
{
public:
	bool operator()(int num1, int num2)
	{
		return num1 > num2;
	}
};

void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);

	//默认从小到大
	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	cout << "----------------------------" << endl;

	//使用函数对象改变算法策略,排序从大到小
	sort(v.begin(), v.end(), MyCompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

首先,在 test01() 函数中,我们创建一个整型向量 v,并添加一些数字元素。

然后,我们使用默认的排序算法 std::sort() 对 v 进行排序。由于默认排序策略为从小到大排序,所以输出为 10 20 30 40 50。

接下来,我们使用函数对象 MyCompare 作为参数传递给 std::sort() 来改变排序策略。在 MyCompare 中,我们重载了函数调用操作符 operator(),并实现了自定义的排序规则,即按照从大到小的顺序进行排序。注意MyCompare要带()

通过使用函数对象 MyCompare(),我们重新对向量 v 进行排序,这次的输出结果为 50 40 30 20 10,与上一次排序的结果相反,符合我们设定的从大到小排序策略。

最后,我们在 main() 函数中调用 test01() 函数来测试排序结果

内建函数对象(Builtin Function Objects)

内建函数对象概念

内建函数对象是由C++标准库提供的预定义函数对象。这些函数对象通常用于算法和容器中,可以提供各种功能,如排序、查找、计数等,它们可以用于常见的算术和逻辑操作。这些内建的函数对象通常是通过函数模板类来实现的,并以特定的名称提供和使用。

注意

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建函数对象,需要引入头文件 #include<functional>

算术仿函数——实现四则运算

其中negate是一元运算,其他都是二元运算

仿函数原型
  • template<class T> T plus<T> //加法仿函数
  • template<class T> T minus<T> //减法仿函数
  • template<class T> T multiplies<T> //乘法仿函数
  • template<class T> T divides<T> //除法仿函数
  • template<class T> T modulus<T> //取模仿函数
  • template<class T> T negate<T> //取反仿函数
示例
#include <functional>
//negate
void test01()
{
	negate<int> n;
	cout << n(50) << endl;
}

//plus
void test02()
{
	plus<int> p;
	cout << p(10, 20) << endl;
}

int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

关系仿函数——实现关系对比

仿函数原型
  • template<class T> bool equal_to<T> //等于
  • template<class T> bool not_equal_to<T> //不等于
  • template<class T> bool greater<T> //大于
  • template<class T> bool greater_equal<T> //大于等于
  • template<class T> bool less<T> //小于
  • template<class T> bool less_equal<T> //小于等于
示例
#include <functional>
#include <vector>
#include <algorithm>

class MyCompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};
void test01()
{
	vector<int> v;

	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(40);
	v.push_back(20);

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	//自己实现仿函数
	//sort(v.begin(), v.end(), MyCompare());
	//STL内建仿函数  大于仿函数
	sort(v.begin(), v.end(), greater<int>());

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

逻辑仿函数——实现逻辑运算

函数原型
  • template<class T> bool logical_and<T> //逻辑与
  • template<class T> bool logical_or<T> //逻辑或
  • template<class T> bool logical_not<T> //逻辑非
示例
#include <vector>
#include <functional>
#include <algorithm>
void test01()
{
	vector<bool> v;
	v.push_back(true);
	v.push_back(false);
	v.push_back(true);
	v.push_back(false);

	for (vector<bool>::iterator it = v.begin();it!= v.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//逻辑非  将v容器搬运到v2中,并执行逻辑非运算
	vector<bool> v2;
	v2.resize(v.size());
	transform(v.begin(), v.end(),  v2.begin(), logical_not<bool>());
	for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_74921567/article/details/132256996