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

📄 safearray.cpp

📁 为SSD5课程《数据结构与算法》中的练习
💻 CPP
字号:
#include "safearray.h"

using namespace std;

//Single param constructor which initialize the variable
template <class T>
safearray<T>::safearray(int initialcapacity)
{
	capacity = initialcapacity;
	storage = new T[size];
}

//Destructor which deallocate this memory
template <class T>
safearray<T>::~safearray()
{
    delete []storage;
}

/*This method provides random element access.
  Throw an out_of_range exception if an attempt is made to 
  access an index outside the valid range*/
template <class T>
T& safearray<T>::operator[](int n) throw(out_of_range)
{
   if(n < 0)
	{
	    throw out_of_range("Index is below 0");
	}

	else if(n > capacity-1)
	{
		throw out_of_range("Index is too high");
	}

	else
			  
		return storage[n];
}

⌨️ 快捷键说明

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