利用模板申請二維數組和釋放

/***********************************************************************
Function:       // NEW2D
Description:    // allocate space for a 2-d matrix
Input:          // i: first index
				// j: second index                                                                      
***********************************************************************/
template <typename T>  
T** NEW2D(int i, int j)
{
	T** buf = new T* [i];
	for(int k=0; k<i; k++)
		buf[k] = new T [j];
	return buf;
}

/***********************************************************************
Function:       // DEL2D
Description:    // delete space for a 2-d matrix
Input:          // i: first index
				// j: second index                                                                      
***********************************************************************/
template <typename T> 
void DEL2D(T ** buf, int i)
{
	for(int j=0; j<i; j++)
		delete [] buf[j];
	delete [] buf;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章