#include <iostream>
template<typename T>
void ShowArray(T arr[], int n);
template<typename T>
void ShowArray(T* arr[], int n);
template<typename T>
T SumArray(T arr[], int n);
template<typename T>
T SumArray(T* arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
int things[] { 13,31,103,301,310,130 };
struct debts mr_E[3] = {
{"Ima Wolfe", 2400.00},
{"Ura Foxe", 1300.00},
{"Iby Stout", 1800.00}
};
double* pd[3];
for (size_t i{}; i != 3; ++i)
{
pd[i] = &(mr_E[i].amount);
}
std::cout << "things的元素为:" << std::endl;
ShowArray(things, 6);
std::cout << "结构体mr_E中的debts为:" << std::endl;
ShowArray(pd, 3);
std::cout << "things的总和为:" << SumArray(things, 6) << std::endl;
std::cout << "pd的总和为:" << SumArray(pd, 3) << std::endl;
return 0;
}
template<typename T>
void ShowArray(T arr[], int n)
{
std::cout << "模板 A" << std::endl;
for (size_t i{}; i != n; ++i)
{
std::cout << arr[i] << ' ';
}
std::cout << std::endl;
}
template<typename T>
void ShowArray(T* arr[], int n)
{
std::cout << "模板 B" << std::endl;
for (size_t i{}; i != n; ++i)
{
std::cout << *arr[i] << ' ';
}
std::cout << std::endl;
}
template<typename T>
T SumArray(T arr[], int n)
{
T sum{};
for (size_t i{}; i != n; ++i)
{
sum += arr[i];
}
return sum;
}
template<typename T>
T SumArray(T* arr[], int n)
{
T sum = *arr[0] - *arr[0]; // 此处技巧需要注意
for (size_t i{}; i != n; ++i)
{
sum += *arr[i];
}
return sum;
}