C++ 仿函数
一. 仿函数(Functor)
仿函数(functor)又称为函数对象(function object)是一个能行使函数功能的类。仿函数的语法几乎和我们普通的函数调用一样,不过作为仿函数的类,都必须重载operator()运算符,举个例子:
1 class Func{ 2 public: 3 void operator() (const string& str) const { 4 cout<<str<<endl; 5 } 6 };
用仿函数实现迭代增量:
1 // C++ program to demonstrate working of 2 // functors. 3 #include <bits/stdc++.h> 4 using namespace std; 5 6 // A Functor 7 class increment 8 { 9 private: 10 int num; 11 public: 12 increment(int n) : num(n) { } 13 14 // This operator overloading enables calling 15 // operator function () on objects of increment 16 int operator () (int arr_num) const { 17 return num + arr_num; 18 } 19 }; 20 21 // Driver code 22 int main() 23 { 24 int arr[] = {1, 2, 3, 4, 5}; 25 int n = sizeof(arr)/sizeof(arr[0]); 26 int to_add = 5; 27 28 transform(arr, arr+n, arr, increment(to_add)); 29 30 for (int i=0; i<n; i++) 31 cout << arr[i] << " "; 32 }
其中,std::transform :
std::transform在指定的范围内应用于给定的操作,并将结果存储在指定的另一个范围内:
1 template<class InputIt, class OutputIt, class UnaryOperation> 2 OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, 3 UnaryOperation unary_op) 4 { 5 while (first1 != last1) { 6 *d_first++ = unary_op(*first1++); 7 } 8 return d_first; 9 } 10 11 template<class InputIt1, class InputIt2, 12 class OutputIt, class BinaryOperation> 13 OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, 14 OutputIt d_first, BinaryOperation binary_op) 15 { 16 while (first1 != last1) { 17 *d_first++ = binary_op(*first1++, *first2++); 18 } 19 return d_first; 20 }
二. STL中的典型仿函数
std::less ,std::equal_to和 std::greater 可用于比较,典型使用示例:
1 // greater example 2 #include <iostream> // std::cout 3 #include <functional> // std::greater 4 #include <algorithm> // std::sort 5 6 int main () { 7 int numbers[]={20,40,50,10,30}; 8 std::sort (numbers, numbers+5, std::greater<int>()); 9 for (int i=0; i<5; i++) 10 std::cout << numbers[i] << ' '; 11 std::cout << '\n'; 12 return 0; 13 }
三. std::bind1st和std::bind2nd
std::bind1st和std::bind2nd函数用于将一个二元算子转换成一元算子。
示例:
1 #include <iostream> 2 #include <iomanip> 3 #include <algorithm> 4 #include <functional> 5 #include <cmath> 6 #include <cstddef> 7 #include <vector> 8 9 int main() 10 { 11 std::vector<double> a = {0, 30, 45, 60, 90, 180}; 12 std::vector<double> r(a.size()); 13 const double pi = std::acos(-1); // since C++20 use std::numbers::pi 14 15 std::transform(a.begin(), a.end(), r.begin(), 16 std::bind1st(std::multiplies<double>(), pi / 180.)); 17 // an equivalent lambda is: [pi](double a){ return a*pi / 180.; }); 18 19 for(std::size_t n = 0; n < a.size(); ++n) 20 std::cout << std::setw(3) << a[n] << "° = " << std::fixed << r[n] 21 << " rad\n" << std::defaultfloat; 22 }
分类:
C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具