国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

線性表——數組描述

2019-11-06 06:26:26
字體:
來源:轉載
供稿:網友

抽象數據類型(abstract date tpye, ADT) 1.抽象數據類型linearList C++支持兩種類——抽象類和具體類。一個抽象類包含沒有實現的代碼的成員函數。這樣的成員函數成為“虛函數(pure virtual function)”. 具體類是沒有虛函數的類。只有虛函數才可以實例化(只能對具體類建立實例或對象)。但是可以建立抽象類的對象指針。

一個線性表的抽象類:

template<class T>class linearList{ public: virtual ~linearList() { }; //線性表為空,返回true virtual bool empty() const = 0; //返回線性表的元素個數 virtual int size() const = 0; //返回索引為theIndex的元素 virtual T& get(int theIndex) const = 0; //返回元素theElement第一次出現的索引 virtual int indexof(const T& theElement) const = 0; //刪除索引為theIndex的元素 virtual void erase(int theIndex) = 0; //將元素theElement插入索引為theIndex的位置 virtual void insert(int theIndex, const T& theElement) = 0; //線性表插入輸出流out virtual void output(ostream& out) const = 0;};

一個抽象類的派生類,只有實現了基類的所有純虛函數才是具體類,否則依然是抽象類不能實例化。

抽象類linearList的派生類arrayList(具體類) :

template<class T>class arrayList : public linearList<T>{ PRotected: T* element; //存儲線性元素的一維數組 int arrayLength; //一維數組的容量 int listSize; //線性表的元素個數 //索引theIndex無效,則拋出異常 void checkIndex(int theIndex) const; public: //構造函數,復制函數和析構函數 arrayList(int initialCapacity = 10); arrayList(const arrayList<T>&); ~arrayList() { delete [] element; } //DAT方法 bool empty() const { return listSize == 0; } int size() const { return listSize; } T& get(int theIndex) const; int indexof(const T& theElement) const; void erase(int theIndex); void insetr(int theIndex, const T& theElement); void output(ostream& out) const; //其他方法 int capacity() const { return arrayLength; }};

類的具體實現:

template<class T>arrayList<T>::arrayList(int initialCapacity){ if(initialCapacity < 1) { ostringstream s; s << "初始容量 = " << initialCapacity << "必須大于零"; //throw illegalParameterValue(s.str()); } arrayLength = initialCapacity; element = new T[arrayLength]; listSize = 0; }template<class T>arrayList<T>::arrayList(const arrayList<T>& theList){ arrayLength = theList.arrayLength; listSize = theList.listSize; element = new T[arrayLength]; copy(theList.elemet, theList.element + listSize, element);}template<class T>T& arrayList<T>::get(int theIndex) const{ checkIndex(theIndex); return element(theIndex); } template<class T>int arrayList<T>::indexof(const T& theElement) const{ int theIndex = (int) (find(element, element + listSize, theElement) - element); if(theIndex == listSize) { return -1; } else { return theIndex; }}template<class T>void arrayList<T>::erase(int theIndex){ //如果引索不存在,則拋出異常 checkIndex(theIndex); //引索存在,向前移動引索大于theIndex的元素 copy(element + theIndex + 1, element + listSize, element + theIndex); element[--listSize].~T(); //調用析構函數 }template<class T>void arrayList<T>::insetr(int theIndex, const T& theElement) { if(theIndex < 0 || theIndex > listSize) { ostringstream s; s << "索引 = " << theIndex << "索引大小 = " << listSize; throw illeaglIndex(s.str()); } //有效引索,確定數組是否已滿 if(listSize == arrayLength) //數組長度空間已滿,數組長度倍增 { changeLength1D(element, arrayLength, 2 * arrayLength) ; arrayLength *= 2; } //把元素向右移動一個位置 copy_backward(element + theIndex, element + listSize, element + theIndex + 1); element[theIndex] = theElement; listSize++;} template<class T>void arrayList<T>::output(ostream& out) const{ copy(element, element + listSize, ostream_iterator<T>(cout, " "));}template<class T>void arrayList<T>::checkIndex(int theIndex) const{ //確定索引theIndex在 0 到 listSize 之間 if(theIndex < 0 || theIndex >= listSize) { ostringstream s; s << "索引 = " << theIndex << "索引大小 = " << listSize; throw illeaglIndex(s.str()); }}//改變一個一維數組長度template<class T>void changeLength1D(T*& a, int oldLength, int newLength){ if(newLength < 0) cout<< "錯誤" <<endl; T* temp = new T[newLength]; //新數組 int number = min(oldLength, newLength); copy(a, a + number, temp); delete [] a; a = temp; }

庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操作。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玛多县| 淄博市| 大方县| 阿拉善盟| 松滋市| 纳雍县| 申扎县| 阳原县| 东山县| 宁津县| 印江| 十堰市| 德昌县| 济阳县| 腾冲县| 威远县| 大余县| 巴林右旗| 雅安市| 临城县| 龙江县| 宝兴县| 武山县| 潢川县| 花垣县| 西昌市| 沈丘县| 长沙市| 海林市| 新晃| 汉源县| 凤台县| 津南区| 灵川县| 涟源市| 余庆县| 金川县| 呼图壁县| 庆城县| 定州市| 峨边|