STL5:仿函数

本文介绍了C++中的仿函数,一种模仿函数行为的类。通过重载operator(),仿函数可以实现类似函数的功能,用于代码复用。文章详细讲解了在C语言中如何使用函数指针和回调函数,而在C++中则有函数指针、函数模板、仿函数及仿函数模板四种方式。特别地,文章深入探讨了STL中的算术类、关系运算类和逻辑运算类仿函数,给出了多个使用实例,包括使用Boost lambda表达式和C++11的lambda表达式进行操作的例子。
摘要由CSDN通过智能技术生成

1、概念

模仿函数的类,使用方式如同函数。本质是类中重载括弧运算符operator()。

2、场景

不同函数复用相同处理代码。

3、使用

3.1 C语言的处理方式

使用函数指针和回调函数来实现代码复用。例如qsort()

#include <stdio.h>
#include <stdlib.h>

int arr_sort(const void *a,const void *b){
    return *(int *)a-*(int *)b;
}

int main(){
    int arr[5]={4,1,2,5,6};
    qsort(arr,5,sizeof(arr[0]),arr_sort);
    int i;
    for(i=0;i<5;i++){
        printf("%d\n",arr[i]);
    }
    return 0;
}

3.2 C++语言的处理方式

(1)函数指针方式

#include<iostream>
#include<algorithm>

using namespace std;

inline bool Sort(int a,int b){
    return a > b;
}
inline void Display(int a){
    cout << a << endl;
}

int main(){
    int arr[]={4,1,2,5,6};
    sort(arr,arr+5,Sort);
    for_each(arr,arr+5,Display);
    return 0;
}

(2)函数模板方式

#include<iostream>
#include<algorithm>
using namespace std;

template<class T>
inline bool Sort(T const& a,const &b){
    return a<b;
}

template<class T>
inline void Display(T a){
    cout << a << endl;
}

int main(){
    int arr[5]={4,1,2,5,6};
    sort(arr,arr+5,Sort<int>);
    for_each(arr,arr+5,Display<int>);
    return 0;
}

(3)仿函数方式

#include<iostream>
#include<algorithm>
using namespace std;

class Sort{
public:
    bool operator()(int a,int b)const {
        return a >b;
    }
};

class Display{
public:
    void operator()(int a)const {
        cout << a << endl;
    }
};
int main(){
    int arr[5]={4,1,2,5,6};
    sort(arr,arr+5,Sort());
    for_each(arr,arr+5,Display());
    return 0;
}

(4)仿函数模板方式

template<class T>
class Sort{  
public:  
    inline bool operator()(T const& a,T const& b) const {
      return a > b;
    }   
};
template<class T>
class Display{
public:
  inline void operator()(T const& a) const{
    cout << a << endl;
  } 
};
int main()  
{  
    int arr[5] = { 4, 1, 2, 5, 6 }; 
    sort(arr, arr+5,Sort<int>());  
    for_each(arr,arr+5,Display<int>());
    return 0;   
}

4、STL的仿函数

在头文件中定义了如下三类仿函数:

4.1 算术类仿函数

在这里插入图片描述

(1)使用实例1

累积相乘

#include <iostream>
#include <numeric>
#include <vector>
#include <functional>

using namespace std;

int main(){
    int arr[]={1,2,3,4,5};
    vector<int> iv(arr,arr+5);
    cout << accumulate(iv.begin(),iv.end(),1,multiplies<int>()) << endl;
    return 0;
}

Boost lambda表达式

   cout << accumulate(res.begin(),res.end(),1,[](int res,int n){return res*n;}) << endl;

(2)使用实例2

两个数组对应元素相加
#include
#include
#include

using namespace std;

inline void Print(int a){
cout << a << endl;
}

int main(){
int first[]={1,2,3,4,5};
int second[]={10,20,30,40,50};
int results[5];
transform(first,first+5,second,results,std::plus());
for_each(results,results+5,Print);
return 0;
}

lambda表达式

  transform(vec.begin(),vec.end(),res.begin(),vec.begin(),[](int n,int m){return n+m;});

基本实现

template<typename T>
struct plus
{
    T operator()(int a,int b){
        return a+b;
    }
};

template<typename T>
struct multiplies
{
    T operator()(int a,int b){
        return a*b;
    }
};

4.2 关系运算类仿函数

在这里插入图片描述

(1)使用实例

降序排序

#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

inline void Print(int a){
    cout << a << endl;
}

int main(){
    int arr[]={1,2,3,4,5};
    sort(arr,arr+5,greater<int>());
    for_each(arr,arr+5,Print);
    return 0;
}

lambda表达式

sort(arr,arr+5,_1>_2);

基本实现

template<typename T>
struct greater{
    bool operator()(const T& a,const T& b){
        return a>b;
    }
};
template<typename T>
struct less{
    bool operator()(const T& a,const T& b){
        return a<b;
    }
};

4.3 逻辑运算仿函数

在这里插入图片描述

(1)布尔数组整体取反

#include <iostream>
#include <functional>
#include <algorithm>

using namespace std;

inline void Print(bool a ){
    cout << a << endl;
}

int main(){
    bool values[]={true,false,true,false};
    transform(values,values+4,values,logical_not<bool>());
    cout.flags(ios::boolalpha);
    for_each(values,values+4,Print);
    return 0;
}

lambda表达式:

transform (values, values+4, values, !_1);

(2)两个布尔数组对应元素相与

#include <iostream> //cout ,boolalpha
#include <functional>//logical_not
#include <algorithm> //transform

using namespace std;

inline void Print(bool a ){
    cout << a << endl;
}

int main(){
    bool values1[]={true,false,true,false};
    bool values2[]={false,true,false,true};
    bool res[4];
    transform(values1,values1+4,values2,res,logical_and<bool>());
    cout.flags(ios::boolalpha);
    for_each(res,res+4,Print);
    return 0;
}

lambda表达式:

transform(values1, values1+4,values2, result, _1 and _2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值