本文共 961 字,大约阅读时间需要 3 分钟。
图片描述已移除
#include#include using namespace std;template T SumArray(T* array, T* const end) { int tempcount = 0; T ret; for (T* temp = array; temp != end; temp++, tempcount++) { if (!tempcount) { ret = *temp; } else { ret += *temp; } } return ret;}
int main() { string array[4] = {"Tom", "Jack", "Mary", "John"}; cout << SumArray(array, array+4) << endl; int a[4] = {1, 2, 3, 4}; cout << SumArray(a, a+4) << endl; return 0;} // 请在此处补充你的代码T* a, T* b) { int tempcount = 0; T* temp; T ret; for (temp = a; temp != b; temp++, tempcount++) { if (!tempcount) { ret = *temp; } else { ret += *temp; } } return ret;} 最近在学习一些编程技巧,遇到了一个挺有意思的问题,就是关于如何高效地实现一个求和函数。不过在实现的过程中,有点遇到了困难,特别是关于函数的编写和优化方面。今天就来和大家分享一下我的思考过程吧。
转载地址:http://runi.baihongyu.com/