Ad
  • No need for redundant size == 1 check!
  • Also added template
Code
Diff
  • #include <vector>    
    #include <numeric>
    
    template <typename T = double>
    int sum(const std::vector<T>& v) {
       return v.size() == 0   ? -1
              : std::accumulate(v.begin(), v.end(), 0);
    }
    • #include <vector>
    • #include <numeric>
    • int sum(std::vector<int>& v) {
    • template <typename T = double>
    • int sum(const std::vector<T>& v) {
    • return v.size() == 0 ? -1
    • : v.size() == 1 ? v[0]
    • : std::accumulate(v.begin(), v.end(), 0);
    • }