fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. template<typename Iterator>
  7. typename std::iterator_traits<Iterator>::value_type Sum(Iterator beg, Iterator end)
  8. {
  9. typedef typename std::iterator_traits<Iterator>::value_type iter_traits_value;
  10. iter_traits_value sum = iter_traits_value();
  11. while(beg != end)
  12. {
  13. sum += *beg;
  14. ++beg;
  15. }
  16. return sum;
  17. }
  18.  
  19. int main() {
  20. int testArray[5] = {0,1,2,3,4};
  21. vector<double> v = {0, 1, 2, 3, 4};
  22. vector<string> s = {"hello", " ", "world"};
  23.  
  24. cout << Sum(v.begin(), v.end()), cout << endl;
  25. cout << Sum(testArray, testArray + sizeof(testArray)/sizeof(testArray[0])), cout << endl;
  26. cout << Sum(s.begin(), s.end()), cout << endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
10
10
hello wolrd