博客
关于我
C++面向对象程序设计 027:简单的SumArray ---- (北大Mooc) (麻了 今天一天嗯是上课没跑成)
阅读量:183 次
发布时间:2019-02-28

本文共 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;}

代码实现

SumPrint 函数实现

// 请在此处补充你的代码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/

你可能感兴趣的文章
poj1190生日蛋糕
查看>>
POJ1218 HDU1337 ZOJ1350 UVALive2557 THE DRUNK JAILER
查看>>
poj1222 EXTENDED LIGHTS OUT(gauss)
查看>>
POJ1240 m叉树
查看>>
Poj1328--Radar Installation(区间选点)
查看>>
POJ1384Piggy-Bank(DP)
查看>>
POJ1417 True Liars —— 并查集 + DP
查看>>
Poj1459 Power Network 预流推进
查看>>
POJ1502(MPI Maelstrom)
查看>>
poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]
查看>>
poj1730 - Perfect Pth Powers(完全平方数)(水题)
查看>>
poj1753——Flip Game
查看>>
poj1936 假期计划第一水
查看>>
poj1958-汉诺四塔问题(三种方法)
查看>>
poj1988(并查集)
查看>>
POJ2007+几何+极角排序
查看>>
poj2039
查看>>
poj2135(简单的最小费用流问题)
查看>>
poj2195 bfs+最小权匹配
查看>>
POJ2251
查看>>