• Source
    1. #include <iostream>
    2. #include <numeric>
    3. #include <vector>
    4.  
    5. #define fold(a,x,f) std::accumulate(begin(a),end(a),(x),(f))
    6. template<class T>typename T::value_type join(T const& c){typedef typename T::value_type U;return fold(c,U(),std::plus<U>());};
    7.  
    8. using namespace std;
    9.  
    10. int main() {
    11. vector<int> a = {10, 20, 30};
    12. vector<string> b = {"hel", "lo wo", "rld"};
    13. cout << join(a) << endl;
    14. cout << join(b) << endl;
    15. cout << fold(a, 0, [](int x, int y){return x+y;}) << endl;
    16. cout << fold(b, string(), [](string x, string y){return x+y;}) << endl;
    17. return 0;
    18. }