基本資料結構(1) —— 動態數組

來源:互聯網
上載者:User

我們設計動態數組的目的在於,利用C++模板技術,消除C++語言中對數組的種種限制,如數組無法成為函數的參數或傳回值(除非是指標),數組無法直接賦值等等。

1 一維動態數組

 

一維動態數組將一個普通數組封裝成data:

 

來看具體實現:

 

template <class T>class Array{protected:T* data;unsigned int base;unsigned int length;public:Array ();Array (unsigned int, unsigned int = 0);~Array ();Array (Array const&);Array& operator = (Array const&);T const& operator [] (unsigned int) const;T& operator [] (unsigned int);T const* Data () const;unsigned int Base () const;unsigned int Length () const;void SetBase (unsigned int);void SetLength (unsigned int);};template <class T>Array<T>::Array() :data (new T [0]),base (0),length (0){}template <class T>Array<T>::Array (unsigned int n, unsigned int m) :data (new T [n]),base (m),length (n){}template <class T>Array<T>::Array (Array<T> const& array) :data (new T [array.length]),base (array.base),length (array.length){for (unsigned int i = 0; i < length; ++i){data [i] = array.data [i];}}template <class T>Array<T>::~Array (){ delete [] data; }template <class T>T const* Array<T>::Data() const{ return data; }template <class T>unsigned int Array<T>::Base() const{ return base; }template <class T>unsigned int Array<T>::Length() const{ return length; }template <class T>T const& Array<T>::operator [] (unsigned int position) const{unsigned int const offset = position - base;if (offset >= length){throw out_of_range ("invalid position");}return data [offset];}template <class T>T& Array<T>::operator [] (unsigned int position){unsigned int const offset = position - base;if (offset >= length){throw out_of_range ("invalid position");}return data [offset];}template <class T>void Array<T>::SetBase (unsigned int newBase){ base = newBase; }template <class T>void Array<T>::SetLength (unsigned int newLength){T* const newData = new T [newLength];unsigned int const min =length < newLength ? length : newLength;for (unsigned int i = 0; i < min; ++i){newData [i] = data [i];}delete [] data;data = newData;length = newLength;}

2 二維動態數組

 

二維動態數組的實現比一維數組更簡單:

 

template <class T>class Array2D{protected:unsigned int numberOfRows;unsigned int numberOfColumns;Array<T>* array;public:Array2D (unsigned int, unsigned int);//T& Select (unsigned int, unsigned int);Array<T> const& operator[](unsigned int index) const{ //const版本return array[index]; //返回索引處的一維數組對象}Array<T>& operator[](unsigned int index){ //非const版本return array[index]; //返回索引處的一維數組對象}};template <class T>Array2D<T>::Array2D( unsigned int m, unsigned int n ):numberOfRows (m),numberOfColumns (n),array (0){//為一維數組Array<T>分配原始記憶體void* raw=::operator new[](numberOfColumns * sizeof(Array<T>));array=static_cast<Array<T>*>(raw);//用placement new調用建構函式初始化各個元素的記憶體for(std::size_t i=0;i<numberOfColumns;++i)new(array+i) Array<T>(numberOfRows);}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.