栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
      • STL仿函数
        • 1. 算术类仿函数
        • 2. 关系运算类仿函数
        • 3. 逻辑运算仿函数

STL仿函数

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

1. 算术类仿函数
操作仿函数
加plus
减minus
乘multiplies
除divides
取模modulus
取反negate
  • 使用实例 :
    累积相乘
#include   
#include  // accumulate
#include  //    
using namespace std;  
int main(){  
  int arr[]={1,2,3,4,5};  
  vector iv(ia,ia+5);  
  cout<< accumulate(iv.begin(),iv.end(),1,multiplies()) < 
  • Boost lambda 表达式
cout<< accumulate(iv.begin(),iv.end(),1,_1*_2) < 
  • 两个数组对应元素相加
#include   
#include  // for_each() transform()
#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;
}
  • Boost lambda 表达式
transform (first, first+5, second, results, _1+_2);
  • 基本实现
template
struct plus{
    T operator()(int a,int b){
        return a+b;
    }
};
template
struct multiplies{
    T operator()(int a,int b){
        return a*b;
    }
};
2. 关系运算类仿函数
操作仿函数
等于equal_to
不等于not_equal_to
大于greater
大于等于greater_equal
小于less
小于等于less_equal
  • 使用实例
    降序排序
#include   
#include  // for_each() sort()
#include   
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());
  for_each(arr,arr+5,Print);
  return 0;
}
  • Boost lambda 表达式
sort(arr,arr+5,_1>_2);
  • 基本实现
template
struct greater{
    bool operator()(const T& a,const T& b){
        return a>b;
    }
};
template
struct less{
    bool operator()(const T& a,const T& b){
        return a 
3. 逻辑运算仿函数 
操作仿函数
逻辑与logical_and
逻辑或logical_or
逻辑否logical_not
  • 使用实例
    布尔数组整体取反
#include      // cout, boolalpha
#include    // std::logical_not
#include     // 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());
  cout.flags(ios::boolalpha);
  for_each(values, values+4,Print);
  return 0;
}
  • Boost lambda 表达式
transform (values, values+4, values, !_1);
  • 使用实例
    两个布尔数组对应元素相与
#include      // cout, boolalpha
#include    // std::logical_not
#include     // 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());
  cout.flags(ios::boolalpha);
  for_each(result,result+4,Print);
  return 0;
}


  • Boost lambda 表达式
transform(values1, values1+4,values2, result, _1 and _2);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009631.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号