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;
}




作者:艾孜爾江


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM