⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e02-04.cpp

📁 游戏开发数据结构Data Structures for Game Programmers
💻 CPP
字号:
// =======================================================
//  Chapter 2, Example 4
//  Using Values as template parameters
// =======================================================
#include <iostream.h>


// -------------------------------------------------------
// Name:        Array
// Description: A simple static array class
// -------------------------------------------------------
template< class Datatype, int size >
class Array
{
public:

    // set function, sets an index
    void Set( Datatype p_item, int p_index )
    {
        m_array[p_index] = p_item;
    }

    // get function, gets an index
    Datatype Get( int p_index )
    {
        return m_array[p_index];
    }

private:

    // the array.
    Datatype m_array[size];
};


void main()
{
    Array<int, 5> iarray5;
    Array<int, 10> iarray10;
    Array<float, 15> farray15;

    // set various indexes in the arrays
    iarray5.Set( 10, 0 );
    iarray5.Set( 3, 1 );
    iarray10.Set( 11, 9 );
    iarray10.Set( 2, 4 );
    farray15.Set( 10.1f, 3 );
    farray15.Set( 3.1415f, 14 );

    // retrieve the indexes again.
    cout << "iarray5.Get( 0 ) = " << iarray5.Get( 0 ) << endl;
    cout << "iarray5.Get( 1 ) = " << iarray5.Get( 1 ) << endl;
    cout << "iarray10.Get( 9 ) = " << iarray10.Get( 9 ) << endl;
    cout << "iarray10.Get( 4 ) = " << iarray10.Get( 4 ) << endl;
    cout << "farray15.Get( 3 ) = " << farray15.Get( 3 ) << endl;
    cout << "farray15.Get( 14 ) = " << farray15.Get( 14 ) << endl;
}

⌨️ 快捷键说明

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