e02-03.cpp

来自「游戏开发数据结构Data Structures for Game Program」· C++ 代码 · 共 35 行

CPP
35
字号
// =======================================================
//  Chapter 2, Example 3
//  Using Multiple template parameters
// =======================================================
#include <iostream.h>


// -------------------------------------------------------
// Name:        Average
// Description: averages an array of data.
// -------------------------------------------------------
template< class Sumtype, class Averagetype >
Averagetype Average( Sumtype* p_array, Averagetype p_count )
{
    int index;
    Sumtype sum = 0;

    // loop through the array and add all the cells
    for( index = 0; p_count > index; index++ )
        sum += p_array[index];

    // divide the sum by the count and return it.
    return (Averagetype)sum / p_count;
}




void main()
{
 int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 cout << "Average( array, 10 ) = " << Average( array, 10 ) << endl;
 cout << "Average( array, 10.0f ) = " << Average( array, 10.0f ) << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?