写一个简单的累加模板,如下:
template<typename T>
T accum(T const* beg, T const* end)
{
T total{
};
while(beg != end)
{
total += * beg;
++ beg;
}
return total;
}
int main()
{
int num[] = {
1, 2, 3, 4, 5 };
std::cout << "the average value of the integer values is "
<< accum(num, num + 5) / 5
<<std::endl;
char name[] = "templates";
int length = sizeof(name) - 1;// (try to) print average character value
std::cout << "the average value of the characters in \""
<< name << "\" is "
<< accum(name, name + length) / length << std::endl;
return 0;