动态数组类

在C++ 编程中,常常会使用到数组,对于动态数组的使用,往往是需要比较谨慎的,比如动态数组使用后要释放内存,动态数组在查找数据时如果超出索引范围,可能会导致程序崩溃等等,因此整理了一个二维、三维动态数组创建的类,可以完成数组的声明,数组中值的设置和数组中数的获取,在使用完后,自动在析构函数中释放内存,具体代码如下:

Array.h

#pragma once
class ArrayG
{
private:
	int X_Length;
	int Y_Length;
	int Z_Length;
public:
	double ***Handle3;
	double **Handle2;
	ArrayG(int xLength,int yLength,int zLength);
	~ArrayG(void);
	double ***CreateArray3();
	double **CreateArray2();
	void deleteArray();
	double getValue(int x,int y,int z);
	double getValue(int x,int y);
	CString setValue(int x,int y,int z,double valueA);
	CString setValue(int x,int y,double valueA);
};

ArrayG.cpp

#include "StdAfx.h"
#include "ArrayG.h"

ArrayG::~ArrayG(void)
{
	this->deleteArray();
	this->X_Length = 0;
	this->Y_Length = 0;
	this->Z_Length = 0;
}

ArrayG::ArrayG(int xLength,int yLength,int zLength)
{
	this->X_Length = xLength;
	this->Y_Length = yLength;
	this->Z_Length = zLength;

	if (zLength > 0)
	{
		this->CreateArray3();
	}
	else
	{
		this->CreateArray2();
	}
}

double ***ArrayG::CreateArray3()
{
	this->Handle3 = new double **[X_Length];
	for (int x = 0;x < X_Length; x++)
	{
		this->Handle3[x] = new double *[Y_Length];
		for (int y = 0; y < Y_Length; y++)
		{
			this->Handle3[x][y] = new double [Z_Length];
		}
	}
	return this->Handle3;
}

double **ArrayG::CreateArray2()
{
	this->Handle2 = new double *[X_Length];
	for (int x = 0;x < X_Length; x++)
	{
		this->Handle2[x] = new double [Y_Length];
		
	}
	return this->Handle2;
}

void ArrayG::deleteArray()
{
	if (this->Z_Length > 0)//三维
	{
		for (int x = 0; x < this->X_Length; x++)//删除z
		{
			for (int y = 0; y < this->Y_Length; y++)
			{
				delete [] this->Handle3[x][y];
			}
		}
		for (int x = 0; x < this->X_Length; x++)//释放y
		{
			delete [] this->Handle3[x];
		}
		delete [] this->Handle3;//释放x
	}
	else//二维
	{
		for (int x = 0; x < this->X_Length; x++)//释放y
		{
			delete [] this->Handle2[x];
		}
		delete [] this->Handle2;//释放x
	}
}

double ArrayG::getValue(int x, int y, int z)
{
	if ( (x < this->X_Length) && (y < Y_Length) && (z < Z_Length))
	{
		return this->Handle3[x][y][z];
	}
	else
	{
		AfxMessageBox(_T("超出索引范围"));
		return -1;
	}
}

double ArrayG::getValue(int x, int y)
{

	if ( (x < this->X_Length) && (y < Y_Length))
	{
		return this->Handle2[x][y];
	}
	else
		AfxMessageBox(_T("超出索引范围"));
	return -1;
}

CString ArrayG::setValue(int x,int y,int z,double valueA)
{
	if( (x < this->X_Length) && (y < Y_Length) && (z < Z_Length))
	{
		this->Handle3[x][y][z] = valueA;
		return _T("1");
	}
	else
	{
		AfxMessageBox(_T("超出索引范围"));
		return _T("-1");
	}
}

CString ArrayG::setValue(int x,int y,double valueA)
{
	if((x < this->X_Length) && (y < Y_Length))
	{
		this->Handle2[x][y] = valueA;
		return _T("1");
	}
	else
	{
		AfxMessageBox(_T("超出索引范围"));
		return _T("-1");
	}
}

应用函数

int X_Length = 5, Y_Length = 4, Z_Length = 3;
ArrayG arrayTest(X_Length,Y_Length,Z_Length);

......

for (int i = 0;i < X_Length; i++)
{
		for (int j = 0; j < Y_Length; j++)
		{
			for (int k = 0; k < Z_Length; k++)
			{

				arrayTest.setValue(i,j,k,i+j+k);//设置参数
                                arrayTest.getValue(i,j,k);//获取参数
                         }
                }
 }
    
......

原文地址:https://www.cnblogs.com/gaozihan/p/10782729.html