/*******************************************
功能: 利用vector对象输出函数结果
作者: gnehoaix
时间: 2019\5\7
********************************************/
#include "stdafx.h"
#include <vector>
#include<cmath>
#include<string>
#include <iostream>
using namespace std;
int main()
{
int GetAdd(int a, int b);
int GetSub(int a, int b);
int GetMul(int a, int b);
int GetDiv(int a, int b);
typedef int (*p)(int, int); // 利用指针p指向该类型的函数
vector<p> a{ GetAdd, GetSub, GetMul,GetDiv}; // vector对象a的元素都是指针
for (auto f : a)
cout << f(2,4) << endl;
return 0;
}
int GetAdd(int a, int b)
{
return a + b;
}
int GetSub(int a, int b)
{
return a - b;
}
int GetMul(int a, int b)
{
return a * b;
}
int GetDiv(int a, int b)
{
return b != 0 ? a / b : 0 ;
}
利用vector对象输出函数结果.cpp
最新推荐文章于 2023-09-05 10:37:03 发布