C++笔记 STL 仿函数

        最近再看STL源码的时候看到里面的实现用了大量的仿函数,然后上网搜集了一些关于仿函数的知识。

         Functor(仿函数), 或者称之为function object(函数对象), 是STL的四大组件之一。

         仿函数是一个能行使函数功能的类

   

本质:所谓的仿函数(functor),是通过重载()运算符模拟函数形为的类。

        仿函数的语法几乎和我们普通的函数调用一样,不过作为仿函数的类,都必须重载 operator() 运算符。因为调用仿函数,实际上就是通过类对象调用重载后的 operator() 运算符。
        将该“操作”设计为一个仿函数(就语言层面而言是个 class),再以该仿函数产生一个对象,并以此对象作为算法的一个参数,接下来我们就详细讲一下如何设计仿函数。
  因此,这里需要明确两点:
  1 仿函数不是函数,它是个类;
  2 仿函数重载了()运算符,使得它的对你可以像函数那样子调用(代码的形式好像是在调用函数)。

/*  2022 03 30  */

/*  学习目标:  C++  STL  仿函数   */
//  C++仿函数:
//  仿函数是什么?
//  1、仿函数就是类中的成员函数,这个成员函数可以让对象  模仿函数调用 的 行为
/*  仿函数本质:重载括号的一个函数*/



//  什么是函数的行为?     函数名(函数参数)
//  在  C++中可以让类实现  类名(函数参数) 模仿函数调用 的 行为

//  自己写一个仿函数  重载()运算符
//  接触得比较多的两个排序准则是:
//  1、greater<type>()               2、less<type>()
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
void printVector(vector<int> &v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}

class Sum
{
public:
	int operator()(int a, int b) const  //operator 是 C++ 的一个关键字
	{
	
		return a + b;
	
	}
};


template <class _Ty>
class Compare
{
public:
	bool operator()(_Ty first, _Ty second)
	{
		return first > second;
	
	}




};

void test01()
{
	/*  方式1 */
	//  operator()  函数名+() (函数参数)
	//  对象.成员函数()(函数参数)

	/*  1.1  重载函数的显式调用  */
	Sum object;// 对象 object
	cout << object.operator()(1, 2) << endl;  //重载函数的显式调用



											  /* 1.2 重载函数的隐式调用 */
	cout << object(5, 2) << endl;


	/*  方式2 */

	//  类名+()   (函数参数)  这种形态的使用叫做仿函数
	cout << Sum()(500, 2) << endl;


}

void test02()
{

	vector<int> vecData = { 1,2,5,8,9,41,25 };

	// 默认使用
	sort(vecData.begin(), vecData.end());  //从小到大,升序排列
	printVector(vecData);
	sort(vecData.begin(), vecData.end(),less<int>());//从小到大,升序排列
	printVector(vecData);

	sort(vecData.begin(), vecData.end(),greater <int>());//降序排列
	printVector(vecData);



	//  自己定义一个仿函数
	sort(vecData.begin(), vecData.end(), Compare <int>());//降序排列
	printVector(vecData);

}


//  标准库里面的仿函数
void test03()
{
	//  包含头文件  #include<functional>
	// 1、算术类  两整数之间
	cout << plus<int>()(1, 4) << endl;
	cout << minus<int>()(1, 4) << endl;
	cout << multiplies<int>()(1, -5) << endl;
	cout << divides<int>()(4, 3) << endl;

	// 2、关系类的运算仿函数    返回布尔类型
	//等于      equal_to
	//大于      greater
	//小于      less
	//不等于    not_equal_to
	//大于等于  greater_equal
	//小于等于  less_equal

	cout << equal_to<int>()(4, 5) << endl;

	// 3、逻辑运算符
	// 与或非
	
	cout << logical_and<int>()(1, 3) << endl;
	cout << logical_or<int>()(1, 3) << endl;
	cout << logical_not<int>()(1) << endl;

	// 4、 位运算 位与,位或,按位取反
	cout << bit_and<int>()(1, 3) << endl;
	//5、选择,证同,投射 (高深方向)





}
int main()
{

	//test01();
	//test02();
	test03();






	system("pause");
	return 0;
}














  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love coldplay

你的鼓励,将让我持续更新

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值