函数指针的几种用法
#include<iostream> #include<vector> using namespace std; void add(int a, int b) { cout<<a + b<<" "; } void ForEach(const vector<int>& nums,void (*add)(int a,int b)){ for (int i = 0; i < nums.size() - 1; i++) { add(nums[i], nums[i + 1]); } } int main() { vector<int> nums{ 2,4,5,6,7,8 }; auto function = add;//这里auto的类型是void(*)(int,int) void(*lee)(int, int) = add;//这里的lee就代表名字,可以用lee替换add typedef void(*AddFunction)(int,int);//此处AddFunction就是这种函数指针类型的别名 AddFunction addfunction = add; ForEach(nums, lee); ForEach(nums, function); ForEach(nums, add); ForEach(nums, addfunction ); }
标签:lee,nums,int,void,add,函数指针 From: https://www.cnblogs.com/hailanben/p/17262666.html