1. 创建一个二维数组:
template<class T>
void make2dArray(T** &x, int numberOfRows, int numberOfColumns)
{
x= new T*[numberOfRows];
for( int i=0; i < numberOfRows; i++)
{
x[i] = new int[numberOfColumns];
}
}
2. 改变一维数组大小
template<class T> void changeLength1D(T*& a, int oldLength, int newLength) { if (newLength < 0) throw invalid_argument("new length must be >= 0"); T* temp = new T[newLength]; // new array int number = min(oldLength, newLength); // number to copy copy(a, a + number, temp); delete [] a; // deallocate old memory a = temp; }
3.改变二维数组大小
template <class T>
void changeLength2D(T** &a, int oldRow, int oldColumn, int newRow, int newColumn)
{
if (newRow < 0 || newColumn < 0)
{
throw invalid_argument("new row and column must be >=0");
}
T** temp = new T*[newRow];
for (int i = 0; i < newRow; i++)
{
temp[i] = new T[newColumn];
}
int co_number = min(oldColumn, newColumn);
int row_number = min(oldRow, oldColumn);
for (int i = 0; i < row_number; i++)
{
copy(a[i], a[i] + co_number, temp[i]);
}
for (int i = 0; i < oldRow; i++)
{
delete[] a[i];
}
delete[] a;
a = temp;
}
4.使用递归函数生成排列
template <class T>
void perm