C/C++四种方法实现加法操作_艾孜尔江撰

/*************************************************************************
	> File Name: add.cpp
	> Author: Alexander
	> Mail: mysoft@111.com
	> Created Time: 一  8/24 20:16:39 2020
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;

int add1(int a, int b) {
    return a + b;
}

class ADD {
public :
    int operator()(int a, int b) {
        return a + b;
    }
};

//  泛型编程
template<typename T, typename U>
auto add3(T a, U b) -> decltype(a + b) {
    return a + b;
}

// 函数式编程
auto add4 = [](int a, int b) -> int {
    return a + b;
};

int arr(int x) {
    return 2 * x;
}

int main() {
    ADD add2;
    cout << add1(3, 4) << endl;
    cout << add2(3, 4) << endl;
    cout << add3(3, 4) << endl;
    cout << add4(3, 4) << endl;
    
    int f[10];
    f[3];
    arr(3);
    return 0;
}




作者:艾孜尔江

posted @ 2021-06-24 10:45  艾孜尔江  阅读(598)  评论(0编辑  收藏  举报