基本資料結構(1) 動態陣列

2021-06-06 03:40:29 字數 1489 閱讀 3510

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

1 一維動態陣列

一維動態陣列將乙個普通陣列封裝成data:

來看具體實現:

template class array

;template array::array() :

data (new t [0]),

base (0),

length (0)

{}template array::array (unsigned int n, unsigned int m) :

data (new t [n]),

base (m),

length (n)

{}template array::array (arrayconst& array) :

data (new t [array.length]),

base (array.base),

length (array.length)

}template array::~array ()

template t const* array::data() const

template unsigned int array::base() const

template unsigned int array::length() const

template t const& array::operator (unsigned int position) const

return data [offset];

}template t& array::operator (unsigned int position)

return data [offset];

}template void array::setbase (unsigned int newbase)

template void array::setlength (unsigned int newlength)

delete data;

data = newdata;

length = newlength;

}

2 二維動態陣列

二維動態陣列的實現比一維陣列更簡單:

template class array2d

array& operator(unsigned int index)

};template array2d::array2d( unsigned int m, unsigned int n ):

numberofrows (m),

numberofcolumns (n),

array (0)

redis基本資料結構 1

redis的作者為了方便自己的使用,在redis中定義了動態字串sds,鍊錶,字典dict,跳躍表skiplist,整數集合intset和壓縮列表ziplist這六種資料結構。下文,我簡要地介紹一下幾種資料結構的定義。sds的全稱叫 dynamic string,它的定義和注釋如下 struct s...

R in Action 1 基本資料結構

一資料型別 1 向量操作 1 a 2 b one two three 3 c 上面把3列向量賦與a,b,c,注意這裡向量內的元素必須是同一種資料型別 數值型 字元型和邏輯型 訪問向量如a c 2,4 將會得到a 2 跟a 4 的值,注意r與c python不一樣,下標訪問從1開始。a 2 4 得到a...

資料結構入門學習系列 1 基本資料結構

在做專案的經驗中發現資料結構與演算法基本決定了乙個程式設計師所能達到的上限,因為最新發現技術上遭遇瓶頸,無法有效提公升最終是由於資料結構與演算法的限制。所以想從頭學習一遍資料結構與演算法,該系列可以作為入門級教程。新手參考,大牛就不用浪費時間看了。首先資料結構包含以下幾種基本關係 邏輯結構 集合 資...