函数指针与函数指针数组的简单理解以及vector

#include <iostream>

#include <vector>

using namespace std;


//start
int add(int const &a, int const &b)
{
int temp = 0;
temp = a + b;
return temp;
}
int sub(int const &a, int const &b)
{
int temp = 0;
temp = a - b;
return temp;
}
int mult(int const &a, int const &b)
{
int temp = 0;
temp = a  * b;
return temp;
}
int divi(int const &a, int const &b)
{
int temp = 0;
temp = a / b;
return temp;

}


int main()
{
int(*p) (int const &a, int const &b);
p = add;//p 为函数指针 ,指向add 函数
//p = &add;   与上面句 是等效的
vector <int(*) (int const &a, int const &b)> v1;//v1容器存放着 元素 为 函数指针,(函数的形参int const &a, int const &b。。。返回值为int)

int(*pb[4])(int const &a, int const &b) = {add,sub,mult,divi};//pb  为 函数指针数组


v1.push_back(add);
//v1.push_back(pb[0])  与上面句 是等效的
int bbb = pb[0](1, 2);
cout << bbb << endl;//3

getchar();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Teleger

你的支持是我前进的方向

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

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

打赏作者

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

抵扣说明:

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

余额充值