template <typename T> T sum(const vector<T> &v, const T &zero = 0);and then make the local variable result be initialised to the value of zero. Notice that the default value for zero means that sum() can still be called in the same way as before the modification. Compile and run test_sum.cc and notice that it still crashes. Now modify test_sum.cc so that sum(vs) is instead sum(vs, string()). Compile and run test_sum.cc and verify that it now works.
int main() { vector<int> v; for (int i = 1; i <= 10; ++i) v.push_back(i); double_copy(v.begin(), v.end(), std::cout); }
T &operator[](size_t i);and should return a reference to the i-th element of the Array object.
Your class should be usable as follows:
#include "Array.h" #include <iostream> int main() { Array<int> a(10); for (int i = 0; i < a.size(); i++) a[i] = i + 1; Array<int> b = a; for (int i = 0; i < a.size(); i++) b[i] *= 2; for (int i = 0; i < a.size(); i++) std::cout << a[i] << " " << b[i] << std::endl; Array<const char*> s(3); s[0] = "one"; s[1] = "two"; s[2] = "three"; for (int i = 0; i < s.size(); i++) std::cout << s[i] << std::endl; }