C++中支持原生數(shù)組,但由于原生數(shù)組的天然缺陷(不能獲取長度信息、越界訪問不會報錯...),我們有必要來開發(fā)自己的數(shù)組類,從而解決這些問題。
數(shù)組類的繼承關系如圖:
創(chuàng)新互聯(lián)公司是專業(yè)的德安網(wǎng)站建設公司,德安接單;提供成都網(wǎng)站設計、網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行德安網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
需求分析:
1、由于線性表,不能作為數(shù)組直接使用,我們需要自己實現(xiàn)一個數(shù)組類來代替原生數(shù)組。
2、解決原生數(shù)組越界訪問不會報錯的問題
3、提供數(shù)組的長度信息
template < typename T >
class Array : public Object
{
protected:
T *m_array;
public:
T& operator [] (int index)
T operator [] (int index) const
bool get(int index, const T& e)
bool set(int index, const T& e)
virtual int length(void) = 0;
};
template < typename T >
class Array : public Object
{
protected:
T *m_array;
public:
T& operator [] (int index)
{
if( (index>=0) && (index<length()) )
{
return m_array[index];
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "array index out of range...");
}
}
T operator [] (int index) const
{
return const_cast<Array<T>&>(*this)[index];
}
bool get(int index, const T& e)
{
bool ret = (index>=0) && (index<length());
if( ret )
{
e = m_array[index];
}
return ret;
}
bool set(int index, const T& e)
{
bool ret = (index>=0) && (index<length);
if( ret )
{
m_array[index] = e;
}
return ret;
}
virtual int length(void) = 0;
};
設計要點:
template < typename T, int N >
class StaticArray : public Array<T>
protected:
T m_space[N];
public:
StaticArray()
// 提供拷貝構(gòu)造喊賦值重載函數(shù),實現(xiàn)數(shù)組的拷貝
StaticArray(const StaticArray<T, N>& obj)
T& operator = (const StaticArray<T, N>& obj)
int length(void)
};
template <typename T, int N>
class StaticArray : public Array<T>
{
protected:
T m_space[N];
public:
StaticArray()
{
this->m_array = m_space;
}
StaticArray(const StaticArray<T, N>& obj)
{
this->m_array = m_space;
for(int i=0; i<length();i++) // 數(shù)組元素拷貝
{
m_space[i] = obj.m_space[i];
}
}
T& operator ==(const StaticArray<T, N>& obj)
{
if(this != &obj)
{
this->m_array = m_space;
for(int i=0; i<length();i++)
{
m_space[i] = obj.m_space[i];
}
}
}
int length(void)
{
return N;
}
};
設計要點:類模板
template < typename T >
class DynamicArray : public Array<T>
{
protected:
int m_length;
public:
DynamicArray(int length)
DynamicArray(const DynamicArray<T>& obj)
DynamicArray<T>& operator = (const DynamicArray<T>& obj)
void resize(int length)
~DynamicArray()
};
DynamicArray類中的函數(shù)實現(xiàn)存在重復的邏輯,可以進行代碼優(yōu)化。
重復代碼邏輯的抽象:
— init 函數(shù)中實現(xiàn)對象構(gòu)造時的初始化操作
— copy 函數(shù)負責從堆空間中申請內(nèi)存,并執(zhí)行拷貝構(gòu)造操作
— updata 將指定的堆空間作為內(nèi)部存儲數(shù)組使用
template <typename T>
class DynamicList : public SeqList<T>
{
protected:
int m_capacity;
public:
DynamicList(int capacity)
{
this->m_array = new T[capacity];
if(this->m_array != NULL)
{
this->m_length = 0;
this->m_capacity = capacity;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create DynamicList object ...");
}
}
int capacity()const
{
return m_capacity;
}
void resize(int capacity)
{
if(capacity != m_capacity)
{
T* array = new T[capacity];
if(array != NULL)
{
int length = (this->m_length < capacity ? this->m_length : capacity);
for(int i=0;i<length;i++)
{
array[i] = this->m_array[i];
}
T* temp = this->m_array;
this->m_array = array;
this->m_length = length;
this->m_capacity = capacity;
delete[] temp;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create DynamicList object ...");
}
}
}
~DynamicList()
{
delete[] this->m_array;
}
};
總結(jié):
名稱欄目:數(shù)據(jù)結(jié)構(gòu)(04)_數(shù)組類的實現(xiàn)
網(wǎng)站URL:http://jinyejixie.com/article34/jjgcse.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、、標簽優(yōu)化、企業(yè)網(wǎng)站制作、做網(wǎng)站、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)