鱼C论坛

 找回密码
 立即注册

C++Primer Plus 第六版编程练习8.8-7

已有 435 次阅读2015-7-6 22:46 |个人分类:c++Primer Plus 第6版编程练习笔记| Plus

#include <iostream>

using namespace std;

template<typename T>
void ShowArray(T arr[] ,int n) ; //模版1

template<typename T>
void ShowArray(T *arr[] ,int n) ;  //模版2

struct debts
{
    char name[50] ;
    double amount ;
};

int main()
{
    int things[6] = {13,31,103,301,310,130} ;
    struct debts mr_E[3] =
    {
        {"Im Wolfe",2400.0},
        {"Ura Foxe",1300.0},
        {"Tby Stout",1800.0}
    };

    double *pd[3] ;
    for(int i=0 ;i<3 ;i++)
        pd[i] = &mr_E[i].amount ;
    cout<<"Sum of Mr.E's things:\n" ;
    ShowArray(things,6) ;        //使用模版1

    cout<<"Sum of Mr.e's debts:\n" ;
    ShowArray(pd,3) ;  //使用模版2

    return 0;
}

template<typename T>
void ShowArray(T arr[] ,int n)  //模版1
{
    cout<<"template A\n" ;
    T sum = 0;
    for(int i =0 ;i < n ;i++)
        sum += arr[i] ;
    cout<<sum<<endl ;
}

template<typename T>
void ShowArray(T *arr[] ,int n)   //模版2
{
    T sum=0.0 ;
    cout<<"template B\n" ;
    for(int i=0;i<n;i++)
        sum += *arr[i];
    cout<<sum<<endl ;
}

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-14 02:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部