zoukankan      html  css  js  c++  java
  • C++primer plus第六版课后编程题答案8.7(补)

    忘发了,补上

    8.7

    #include <iostream>
    using namespace std;
    template <typename T>
    T SumArray(T arr[],int n)
    {
    	T sum=0;
    	for(int i=0;i<n;i++)
    		sum+=arr[i];
    	return sum;
    };
    template <typename T>
    T SumArray(T *arr[],int n)//arr是指向指针数组的指针
    {
    		T sum=0;
    		for(int i=0;i<n;i++)
    			sum+=*arr[i];	//arr[i]是一个指针,+*之后才是其中的数值;
    		return sum;
    };
    struct debts{
    	char name[20];
    	double amount;
    };
    
    void main87()
    {
    	int things[6]={13,31,103,301,310,130};
    	struct debts mr_E[3]={
    		{"Ima 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<<"The sum is"<<SumArray(things,6)<<endl;
    	cout<<"The sum is"<<SumArray(pd,3)<<endl;
    
    	//system("cd..");
    	system("pause");
    
    
    }
    


  • 相关阅读:
    HDU 5247
    HDU 4965
    CodeForces 445B
    HDU 5835
    CodeForces 731C
    HDU 5783
    CodeForces 660D
    POJ 1631
    HDU 6112
    HDU 5860
  • 原文地址:https://www.cnblogs.com/qq84435/p/3664825.html
Copyright © 2011-2022 走看看