C++ Primer(第5版) 练习 6.55
练习 6.55 编写4个函数,分别对两个int值执行加、减、乘、除运算;在上一题创建的vector对象中保存指向这些函数的指针。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************> File Name: ex6.55.cpp> Author: > Mail: > Created Time: Wed 14 Feb 2024 09:32:18 PM CST************************************************************************/#include<iostream>
#include<vector>
using namespace std;using F = int(*)(int, int);int add(int a, int b){return a + b;
}int sub(int a, int b){return a - b;
}int multi(int a, int b){return a * b;
}int division(int a, int b){return a / b;
}int main(){vector<F> func = {add, sub, multi, division};int a, b;cout<<"Enter a, b: ";cin>>a>>b;cout<<"sum = "<<func[0](a, b)<<endl;return 0;
}