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

📄 array.cpp

📁 自己做课程设计的时候做的小型编译器
💻 CPP
字号:
#include "stdafx.h"
using namespace std;


template <class T>
void Array<T>::Allocate()
{
	alist=new T[FSize];
	if (alist==0)
		cerr<<"Memory Allocation Fail."<<endl;
}

template <class T>
Array<T>::Array(int sz)
{
	if(sz<=0) 
	{
		cerr<<"Invalid Array Size."<<endl;
		return;
	}
	FSize=sz;
	Allocate();
}

template <class T>
Array<T>::Array(const Array<T> &x)
{
	FSize=x.FSize;
	Allocate();
	for(int i=0;i<FSize;i++)
		alist[i]=x.alist[i];
}

template <class T>
T & Array<T>::operator[](int i)
{
	if(i<0 || i>=FSize)
	{
		cerr<<"Invalid index."<<endl;
		exit(1); 
	}
	return alist[i];
}

template<class T>
void Array<T>::ReSize(int NewSize)
{
	if(NewSize<=0)
	{
		cerr<<"Invalid Array Size."<<endl;
		return;
	}
	if(NewSize!=FSize)
	{
		T* newArray=new T[NewSize];
		if(newArray==0)
		{
			cerr<<"Memory Allocation Fail."<<endl;
			return;
		}
		int n=(NewSize<=FSize?NewSize:FSize);
		for (int i=0;i<n;i++)
			newArray[i]=alist[i];
		delete[] alist;
		alist=newArray;
		FSize=NewSize;
	}
}



⌨️ 快捷键说明

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