功能描述:

    • 实现四则运算
    • 其中negate是一元运算,其他都是二元运算

    仿函数原型:

    • template T plus //加法仿函数
    • template T minus //减法仿函数
    • template T multiplies //乘法仿函数
    • template T divides //除法仿函数
    • template T modulus //取模仿函数
    • template T negate //取反仿函数

    代码演示:

    1. #include<iostream>
    2. using namespace std;
    3. #include<functional>//内建函数对象的头文件
    4. //内建仿函数 算数仿函数
    5. //negate 一元仿函数 (取反)
    6. void test01() {
    7. negate<int>a;
    8. cout << a(15) << endl;//-15
    9. }
    10. //plus 二元仿函数 (加法运算)
    11. void test02() {
    12. plus<int>a;
    13. cout << a(10, 5) << endl;
    14. }
    15. int main() {
    16. test01();
    17. test02();
    18. return 0;
    19. }

    运行结果:
    image.png