博客
关于我
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/

你可能感兴趣的文章
PHP的引用举例
查看>>
PHP相关代码
查看>>
RabbitMQ
查看>>
php知识点记录
查看>>
PHP类数组式访问(ArrayAccess接口)
查看>>
PHP系列:浅谈PHP中isset()和empty() 函数的区别
查看>>
PHP索引数组unset的坑-array_values解决方案
查看>>
PHP索引数组排序方法整理(冒泡、选择、插入、快速)
查看>>
PHP线程安全和非线程安全
查看>>
R3LIVE开源项目常见问题解决方案
查看>>
php缃戠珯,www.wfzwz.com
查看>>
php缓存查询函数
查看>>
php编写TCP服务端和客户端程序
查看>>
php编码规范
查看>>
PHP编码规范-PSR1、psr2 /psr3 psr4
查看>>
PHP编程效率的20个要点
查看>>
PHP网页缓存技术优点及代码
查看>>
PHP自动化测试(一)make test 和 phpt
查看>>
php自定义函数: 文件大小转换成智能形式
查看>>
php英语单词,php常用英语单词,快速学习php编程英语(6)
查看>>