vector容器、迭代器和空間配置器三個類方法的實現
阿新 • • 發佈:2018-11-14
C++的STL庫有一個容器叫vector,這個容器底層的資料結構是一個記憶體可以自動增長的陣列,每次當陣列儲存滿了以後,記憶體可以自動增加兩倍,請完成vector容器、迭代器和空間配置器三個類方法的實現。
#include<iostream> using namespace std; //容器預設的空間配置器的實現 template<typename T> class myallocator { public: T* allocate(size_t size) //開闢記憶體 { return (T*)malloc(size); } void deallocate(void *ptr) //釋放記憶體 { free(ptr); } void construct(T *ptr, const T &val)//構造物件 { new(ptr)T(val); } void destroy(T *ptr) //析構物件 { ptr->~T(); } }; //容器的類模板 template<typename E, typename allocator = myallocator<E>> class Vector { public: //迭代器型別前置宣告 class iterator; //預設建構函式 Vector(int size = 5) { _first = _allocator.allocate(size*sizeof(E)); _end = _first + size; _last = _first; } //解構函式 ~Vector() { int size = _last - _first; for (int i = 0; i < size; ++i) { _allocator.destroy(_first + i); } _allocator.deallocate(_first); _first = _last = _end = NULL; } //拷貝建構函式 Vector(const Vector<E, allocator> &src) { int length = src._end - src._first; _first = _allocator.allocate(length*sizeof(E)); int size = src._last - src._first; for (int i = 0; i < size; ++i) { _allocator.construct(_first + i, src._first[i]); } _end = _first + size; _last = _first + length; } //賦值運算子的過載函式 Vector<E, allocator>& operator=(const Vector<E, allocator> &src) { if (this == &src) { return*this; } int size = _last - _first; for (int i = 0; i < size; ++i) { _allocator.destroy(_first + i); } _allocator.deallocate(_first); _first = _last = _end = NULL; int length = src._end - src._first; _first = _allocator.allocate(length*sizeof(E)); int size = src._last - src._first; for (int i = 0; i < size; ++i) { _allocator.construct(_first + i, src._first[i]); } _end = _first + size; _last = _first + length; } //向容器的末尾新增新元素value,若增長記憶體,呼叫resize函式 void push_back(const E &value) { if (full()) { resize(); } _allocator.construct(_last, value); _last++; } //刪除容器末尾的元素 void pop_back() { if (empty()) { return; } _last--; _allocator.destroy(_last); } //向指定位置插入新元素value,若增長記憶體,呼叫resize函式 iterator insert(iterator it, const E &value) { if (it.mptr<_first || it.mptr>_last) { return _last; } if (_last == _end) { int offset = it.mptr - _first; resize(); it.mptr = _first + offset; } E*p = _last; for (; p > it.mptr; --p) { _allocator.construct(p, *(p - 1)); _allocator.destroy(p - 1); } _allocator.construct(p, value); _last++; return p; } //刪除指定位置的元素 iterator erase(iterator it) { if (it.mptr<_first || it.mptr >= _last) { return _last; } E*p = it.mptr; for (; p < _last - 1; ++p) { _allocator.destroy(p); _allocator.construct(p, *(p + 1)); } _allocator.destroy(p); --_last; return it.mptr; } //判斷容器是否空 bool empty()const{ return _first == _last; } //判斷容器是否滿 bool full()const{ return _last == _end; } //容器的迭代器實現 class iterator { public: iterator(E*ptr = NULL) :mptr(ptr){} bool operator!=(const iterator&it) { return mptr != it.mptr; } void operator++() { mptr++; } E&operator*() { return*mptr; } E*mptr; }; iterator begin(){ return iterator(_first); } iterator end(){ return iterator(_last); } private: //記憶體增長函式,按原有容量的兩倍增長 void resize() { int size = _last - _first; E*ptmp = _allocator.allocate((size)*sizeof(E)* 2); for (int i = 0; i < size; ++i) { _allocator.construct(ptmp + i, _first[i]); } for (int i = 0; i < size; ++i) { _allocator.destroy(_first + i); } _allocator.deallocate(_first); _first = ptmp; _end = _first + size * 2; _last = _first + size; } E *_first; //指向陣列的起始位置 E *_end; //指向陣列的末尾的後繼位置 E *_last; //指向陣列最後一個有效資料的後繼位置 allocator _allocator; // vector容器的空間配置器 }; int main() { Vector<int>Vec; Vec.push_back(20); Vec.push_back(21); Vec.push_back(22); Vec.push_back(23); Vec.push_back(24); Vec.push_back(25); Vec.push_back(26); Vec.pop_back(); Vec.erase(Vec.begin()); Vec.insert(Vec.begin(),66); Vector<int>::iterator it = Vec.begin(); for (; it != Vec.end(); ++it) { cout << *it << " "; } cout << endl; return 0; }
執行結果如下: