05 C++STL之函数对象(仿函数)

STL仿函数

STL中的仿函数的主要功能是为了搭配STL算法使用, 仿函数(functors)在C++标准中采用的名称是函数对象(function objects),在<functional>头文件中定义了如下三类仿函数:

1. 算术类仿函数
操作仿函数
plus<T>
minus<T>
multiplies<T>
divides<T>
取模modulus<T>
取反negate<T>
  • 使用实例 :
    累积相乘
#include <iostream>  
#include <numeric> // accumulate
#include <functional> //    
using namespace std;  
int main(){  
  int arr[]={1,2,3,4,5};  
  vector<int> iv(ia,ia+5);  
  cout<< accumulate(iv.begin(),iv.end(),1,multiplies<int>()) <<endl;   
  return 0;   
}
  • Boost lambda 表达式
cout<< accumulate(iv.begin(),iv.end(),1,_1*_2) <<endl; 
  • 两个数组对应元素相加
#include <iostream>  
#include <algorithm> // for_each() transform()
#include <functional>  
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<int>());
  for_each(results,results+5,Print);
  return 0;
}
  • Boost lambda 表达式
transform (first, first+5, second, results, _1+_2);
  • 基本实现
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;
    }
};
2. 关系运算类仿函数
操作仿函数
等于equal_to<T>
不等于not_equal_to<T>
大于greater<T>
大于等于greater_equal<T>
小于less<T>
小于等于less_equal<T>
  • 使用实例
    降序排序
#include <iostream>  
#include <algorithm> // for_each() sort()
#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;
}
  • Boost 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;
    }
};


3. 逻辑运算仿函数
操作仿函数
逻辑与logical_and<T>
逻辑或logical_or<T>
逻辑否logical_not<T>
  • 使用实例
    布尔数组整体取反
#include <iostream>     // cout, boolalpha
#include <functional>   // std::logical_not
#include <algorithm>    // std::transform

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;
}
  • Boost lambda 表达式
transform (values, values+4, values, !_1);
  • 使用实例
    两个布尔数组对应元素相与
#include <iostream>     // cout, boolalpha
#include <functional>   // std::logical_not
#include <algorithm>    // std::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 result[4];
  transform(values1, values1+4,values2, result, logical_and<bool>());
  cout.flags(ios::boolalpha);
  for_each(result,result+4,Print);
  return 0;
}


  • Boost lambda 表达式
transform(values1, values1+4,values2, result, _1 and _2);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Carson.Yang

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值