safearray.cpp
来自「为SSD5课程《数据结构与算法》中的练习」· C++ 代码 · 共 40 行
CPP
40 行
#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 + =
减小字号Ctrl + -
显示快捷键?