1 #include <iostream> 2 #include <string> 3 using namespace std; 4 template <class T> 5 T SumArray( 6 T *p,T *q){ 7 T sum = *p; 8 while(++ p != q) 9 sum += *p; 10 return sum; 11 } 12 int main() { 13 string array[4] = { "Tom","Jack","Mary","John"}; 14 cout << SumArray(array,array+4) << endl; 15 int a[4] = { 1, 2, 3, 4}; //提示:1+2+3+4 = 10 16 cout << SumArray(a,a+4) << endl; 17 return 0; 18 }