|
发表于 2016-8-3 20:30:09
|
显示全部楼层
本帖最后由 patton88 于 2016-8-3 21:34 编辑
- // 前置声明方式也行
- // VS2013 Win32 Console UNICODE Success Run
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- struct debts
- {
- char name[50];
- double amount;
- };
- template<class T>
- double sumarray(T ar[], int n);
- template<>
- double sumarray<debts>(debts ar[], int n);
- int main()
- {
- int things[6] = { 13, 31, 103, 301, 310, 130 };
- debts ded[3] =
- {
- { "Ima Wlofe", 2400.0 },
- { "Ura Foxe", 1300.0 },
- { "Iby Stout", 1800.0 }
- };
- double *pd[3];
- for (int i = 0; i < 3; i++)
- {
- pd[i] = &ded[i].amount;
- }
- cout << "things zong he:" << sumarray(things, 6) << endl;
- cout << "ded zong he:" << sumarray(ded, 3) << endl;
- system("pause");
- return 0;
- }
- template<class T>
- double sumarray(T ar[], int n)
- {
- double re = ar[0];
- for (int i = 1; i < n; i++)
- re += ar[i];
- return re;
- }
- template<>
- double sumarray(debts ar[], int n)
- {
- double re = ar[0].amount;
- for (int i = 1; i < n; i++)
- re = re + ar[i].amount;
- return re;
- }
- /* Result of Run:
- things zong he:888
- ded zong he:5500
- Press any key to continue . . .
- */
复制代码 |
|