• 【C++函数的进化】函数指针,模板,仿函数,lambda表达式


    /**
     * @poject          
     * @author			jUicE_g2R(qq:3406291309)
     * @file            C++函数的进化
     * 
     * @language        C++
     * @EDA				Base on VS2022
     * @editor			Obsidian(黑曜石笔记软件)
     * 
     * @copyright		2023
     * @COPYRIGHT	    原创学习笔记:转载需获得博主本人同意,且需标明转载源
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 函数进化()
      函数 -> 函数指针 -> 函数模板 -> 仿函数->lambda表达式

    1 函数

    #include 
    using std::cin;
    using std::cout;
    using std::string;
    typedef int* pInt;
    
    int Count_Match20_Elem(pInt pSta, pInt pEnd) {
    	int res = 0;
    	for (; pSta != pEnd; ++pSta)
    		if (*pSta > 20)
    			res++;
    	return res;
    }
    int Count_Match25_Elem(const pInt pSta, const pInt pEnd) { // 这样会造成代码冗余
    	int res = 0;
    	for (; pSta != pEnd; ++pSta)
    		if (*pSta > 25)
    			res++;
    	return res;
    }
    
    int main(int* argc, char* argv[]){
    	int arr[] = { 11,16,21,19,17,30 };
    	cout << Count_Match20_Elem(arr, arr + sizeof(arr) / 4);	// sizeof(arr):得到的是arr数组的 总字节大小 ,而不是arr中元素的个数(一个int元素占4bit)
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2 函数指针

    • 函数 变成 变量
    • “行为” “数据化”
    #include 
    using std::cin;
    using std::cout;
    using std::string;
    
    //将判断处的代码(独特之处)重新封装为函数
    bool isGreater20(const int& val) { return val > 20; }
    bool isGreater25(const int& val) { return val > 25; }
    int CountMatchElem(int* pSta, int* pEnd, bool(*pComp)(const int&)) {
    	int res = 0;
    	for (; pSta != pEnd; ++pSta)
    		if (pComp(*pSta))	//pComp为函数指针;括号里的传入参数为 指针变量pSta指向的对象
    			res++;
    	return res;
    }
    
    int main(int* argc, char* argv[]){
    	int arr[] = { 11,16,21,19,17,30 };
    	cout << CountMatchElem(arr, arr + sizeof(arr)/ sizeof(int), isGreater20);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3 函数模板

    • 独立于类型的函数
    • 可产生函数特定类型的版本
    #include 
    using std::cin;
    using std::cout;
    using std::string;
    
    bool isGreater20(const int& val) { return val > 20; }
    bool isGreater25(const int& val) { return val > 25; }
    bool isTinyStr(const string& str) { return str.size() <= 3; }
    template<typename DataType>
    int CountMatchElem(DataType* pSta, DataType* pEnd, bool(*pComp)(const DataType&)) {
    	int res = 0;
    	for (; pSta != pEnd; ++pSta)
    		if (pComp(*pSta))	//pComp为函数指针;括号里的传入参数为 指针变量pSta指向的对象
    			res++;
    	return res;
    }
    int main(int* argc, char* argv[]){
    	int arr[] = { 11,16,21,19,17,30 };
    	string strs[] = { "abc", "bcde", "cdefg", "de", "efg" };
    	cout << CountMatchElem<int>(arr, arr + sizeof(arr)/ sizeof(int), isGreater20);
    	cout << CountMatchElem<string>(strs, strs + sizeof(strs) / sizeof(strs[0]), isTinyStr);
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4 仿函数(函数对象)

    • 定义了调用操作符
    • 行为类似函数的对象
    #include 
    using std::cin;
    using std::cout;
    using std::string;
    
    template<typename T>
    struct Greater { // 定义仿函数
    	T StdVal;
    	explicit Greater(T val) : StdVal(val) {} // 构造函数初始化StdVal
    	bool operator()(const T& val) const { return val > StdVal; } // 重载函数调用操作符
    };
    
    template<typename DataType>
    int CountMatchElem(DataType* pSta, DataType* pEnd, bool(*pComp)(const DataType&)) {
    	int res = 0;
    	for (; pSta != pEnd; ++pSta)
    		if (pComp(*pSta))	//pComp为函数指针;括号里的传入参数为 指针变量pSta指向的对象
    			res++;
    	return res;
    }
    
    int main(int* argc, char* argv[]){
    	int arr[] = { 11,16,21,19,17,30 };
    	Greater<int> gtr20(20); // 实例化一个函数对象,将判断阈值设为20
    	cout << CountMatchElem(arr, arr + sizeof(arr) / sizeof(int), gtr20); // 这里会报错!!!
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 更改
    template<typename DataType, typename pFunc>
    int CountMatchElem(DataType* pSta, DataType* pEnd, pFunc pComp) 
    
    • 1
    • 2

    5 lambda表达式简化

    #include 
    using std::cin;
    using std::cout;
    using std::string;
    
    template<typename DataType, typename pFunc>
    int CountMatchElem(DataType* pSta, DataType* pEnd, pFunc pComp) {
    	int res = 0;
    	for (; pSta != pEnd; ++pSta)
    		if (pComp(*pSta))	//pComp为函数指针;括号里的传入参数为 指针变量pSta指向的对象
    			res++;
    	return res;
    }
    
    int main(int* argc, char* argv[]){
    	int arr[] = { 11,16,21,19,17,30 };
    	auto gtr20 = [](auto& val) -> bool {return val > 20; };
    	cout << CountMatchElem(arr, arr + sizeof(arr) / sizeof(int), gtr20); // 这里会报错
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    基于LangChain的LLM应用开发3——记忆
    视频推拉流EasyDSS直播点播平台获取指定时间快照的实现方法
    腾讯云CVM服务器操作系统镜像大全
    PyTorch学习笔记-Convolution Layers与Pooling Layers
    js防抖和节流
    尚硅谷Vue系列教程学习笔记(6)
    docker安装jenkins以及Permission denied错误的解决方法!
    49. 视频热度问题
    Vue+element 登录业务实现
    Vue.js vs React vs Angular
  • 原文地址:https://blog.csdn.net/qq_73928885/article/details/134485420