使用類模板實現STL Vector,點擊查看代碼 #include <iostream> using namespace std; template<typename T> class MyVector { public: //構造函數 MyVector<T>(int size = 10) { T ...
使用類模板實現STL Vector,點擊查看代碼
#include <iostream>
using namespace std;
template<typename T>
class MyVector {
public:
//構造函數
MyVector<T>(int size = 10) {
T * _tep = new T[size]();
first = _tep;
last = _tep;
end = first + size;//
cout << "構建Vector,首地址" << first << endl;
}
//拷貝構造
MyVector<T>(const MyVector<T> & _src) {
//對方vector是為空
if (_src.Empty()) {
int srcVectorySize = _src.getVectorSize();
T * _tep = new T[srcVectorySize]();
first = _tep;
last = _tep;
end = first + srcVectorySize;//
cout << "拷貝構造構建Vector,空拷貝" << endl;
}
else {
int srcVectorySize = _src.getVectorSize();
T * _tep = new T[srcVectorySize]();
first = _tep;
last = _tep;
end = first + srcVectorySize;//
T * _srcVectorElementPoint = _src.first;
while (_srcVectorElementPoint < _src.last) {
*_tep = *_srcVectorElementPoint;
_tep++;
_srcVectorElementPoint++;
}//end
last=_tep;
cout << "拷貝構造構建Vector" << endl;
}
}
//賦值函數
MyVector<T> & operator=(const MyVector<T> & _src)
{
//避免重覆
if (this == &_src) { return *this; }
//釋放現有堆上資源空間
if (this->Empty() == false) {
delete[]first;
first = nullptr;
last = nullptr;
end = nullptr;
}
int srcVectorySize = _src.getVectorSize();
T * _tep = new T[srcVectorySize]();
first = _tep;
last = _tep;
end = first + srcVectorySize;//
T * _srcVectorElementPoint = _src.first;
while (_srcVectorElementPoint < _src.last) {
*_tep = *_srcVectorElementPoint;
_tep++;
_srcVectorElementPoint++;
}//end
last = _tep;
cout << "賦值函數構建Vector" << endl;
}
//析構函數
~MyVector<T>() {
if (Empty() == false) {
delete[]first;
first = nullptr;
last = nullptr;
end = nullptr;
cout << "析構Vector,堆地址"<<first << endl;
}
}
//添加值,返回指向當前元素的指針,返回為 const * 不允許修改
const T * pushBack(const T & _srcValue) {
//滿空間,兩倍擴容
if (Full()) {
Expend();
}
*last = _srcValue;
last++;
cout << "Vector添加元素,元素地址" << last << endl;
return last;
}
void popBack(){
this->last--;
}
//獲取指定下標的值
T getValue(int index) const {
if (index<0) { return *first; }
if (index > this->getVectorSize()) { return *(last-1); }
int flag = 0;
T *elementPoint = first;
while (flag < index) {
elementPoint++;
flag++;
}
cout << "獲取Vector元素值,元素地址" << elementPoint <<"元素值"<< *elementPoint << endl;
return *elementPoint;
}
//編輯指定下標元素的值,返回當前節點的指針 不允許通過返回指針修改
const T * eidtValue(int index,const T & _value) {
if (index > this->getVectorSize() || index<0) { return nullptr; }
int flag = 0;
T *elementPoint = first;
while (flag < index) {
elementPoint++;
flag++;
}
*elementPoint = _value;
cout << "編輯Vector元素值,元素地址" << elementPoint << "元素值" << *elementPoint << endl;
return elementPoint;
}
//判斷是否為空
bool Empty() const {
if (first == last) { return true; }
return false;
}
//判斷空間是否滿
bool Full() const{
if (last == end) { return true; }
return false;
}
int getVectorSize() const {
return this->end - this->first;
}
void printVector() const {
cout << "列印數組元素" << endl;
T *elementPoint = first;
int index = 0;
while (elementPoint < last)
{
cout.precision(4);
cout << "[" << index << "]=" << (*elementPoint) <<"該元素地址="<< elementPoint << endl;
elementPoint++;
index++;
}
}
private:
T * first;
T * last;
T * end;
//兩倍擴容
void Expend() {
int size = this->getVectorSize();
int newSize = size * 2;
T *newFirst = new T[newSize];
T *newLast = newFirst;
T *newEnd = newFirst + newSize;
const T *srcElementPoint = this->first;
while (srcElementPoint < this->last) {
*newLast = *srcElementPoint;
newLast++;
srcElementPoint++;
}
//釋放原有空間
delete[]first;
first = nullptr;
last = nullptr;
end = nullptr;
first = newFirst;
last = newLast;
end = newEnd;
cout << "兩倍擴容新堆記憶體地址"<< first << endl;
}
};
int main() {
MyVector<int> v1 (6) ;
v1.pushBack(10);
v1.pushBack(9);
v1.pushBack(8);
v1.pushBack(7);
v1.pushBack(6);
v1.pushBack(5);
v1.printVector();
v1.pushBack(4);
v1.printVector();
v1.eidtValue(2, 100);
v1.printVector();
int getValue = v1.getValue(3);
cout << "getValue =" << getValue << endl;
MyVector<int> v2 = v1;
v2.printVector();
MyVector<int> v3(10);
v3 = v1;
v3.printVector();
system("pause");
return 1;
}