📄 e02-01.cpp
字号:
// =======================================================
// Chapter 2, Example 1
// Summing an array of numbers using template functions
// =======================================================
#include <iostream.h>
// -------------------------------------------------------
// Name: SumIntegers
// Description: returns the sum of an array of integers
// Arguments: - p_array: the array
// - p_count: the number of items in the array
// -------------------------------------------------------
int SumIntegers( int* p_array, int p_count )
{
// declare the index and the sum variables.
int index;
int sum = 0;
// loop through the array and add every cell.
for( index = 0; index < p_count; index++ )
sum += p_array[index];
// return the sum.
return sum;
}
// -------------------------------------------------------
// Name: SumFloats
// Description: returns the sum of an array of floats
// Arguments: - p_array: the array
// - p_count: the number of items in the array
// -------------------------------------------------------
float SumFloats( float* p_array, int p_count )
{
// declare the index and the sum variables.
int index;
float sum = 0;
// loop through the array and add every cell.
for( index = 0; index < p_count; index++ )
sum += p_array[index];
// return the sum
return sum;
}
// -------------------------------------------------------
// Name: Sum
// Description: returns the sum of an array of datatypes
// Arguments: - p_array: the array
// - p_count: the number of items in the array
// -------------------------------------------------------
template< class T >
T Sum( T* p_array, int p_count )
{
// declare the index and the sum variables.
int index;
T sum = 0;
// loop through the array and add every cell.
for( index = 0; index < p_count; index++ )
sum += p_array[index];
// return the sum.
return sum;
}
void main()
{
int intarray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
float floatarray[9] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f,
6.6f, 7.7f, 8.8f, 9.9f };
// first sum the two arrays using the non-templated functions.
cout << "Using SumIntegers, the sum of intarray is: ";
cout << SumIntegers( intarray, 10 ) << endl;
cout << "Using SumFloats, the sum of floatarray is: ";
cout << SumFloats( floatarray, 9 ) << endl;
// now sum the two arrays using the templated function.
cout << "Using Sum, the sum of intarray is: ";
cout << Sum( intarray, 10 ) << endl;
cout << "Using Sum, the sum of floatarray is: ";
cout << Sum( floatarray, 9 ) << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -