<四>理解空間配置器allocator, 優化STL 中的Vector

.在上一節我們實現的 MyVector存在哪些問題?

問題1

現在有Student類
class Student{
   public:
   Student(){cout<<"構造Student對象"<<endl;}
   ~Student(){cout<<"析構Student對象"<<endl;}
   private:
       int   age
       char  NAME[20;]
       char  *pAddress;
}

MyVector<Student> v1[1000];

我只是希望創建一個能放1000個Student 的Vector,但是開始並不放任何內容,但是發現編譯器除分配了1000個student對象的空間,還創建了1000個對象,
在main函數結束後再析構這1000個對象,這就是問題1,這1000個對象並不是我需要的,
原因如下,在MyVector的構造函數中 T * _tep = new T[size](); 這個new除了分配空間,還會調用對象的構造函數完成對象的初始化
換句話說就是 空間的分配和對象的創建聯繫在了一起,這個非常不合理,我們需要把他分開,我希望是需要幫我開闢空間即可,不希望幫我創建1000個對象

問題2關於析構函數

//析構函數
~MyVector<T>() {
	if (Empty() == false) {
		delete[]first;
		first = nullptr;
		last  = nullptr;
		end   = nullptr;
		cout << "析構Vector,堆地址"<<first << endl;
	}

}

實際情況時,我的vector是可以放1000個student對象的容器,但是實際裡面可能暫時只放了100個對象,
而delete[] first,是把整個vector中的1000個student對象都調用析構函數,這也不合理,我們只需要析構有效
數量的對象然後再釋放first指針指向的堆記憶體

問題3 關於添加元素和刪除元素

student s1;
student s2;
student s3;

MyVector<student> v1(1000);// 這一句編譯器會先分配1000個student對象空間,然後再調用1000次構造函數創建1000個student對象


v1.pushback(s1);
//上面這句話會調用賦值函數,會將s1的內容賦值給已經在堆上的student對象,但實際上我想要的是,只需要將分配好的空間
//給我,我使用拷貝構造,將s1拷貝構造出來一個對象放在你給的空間上,這是個問題


v1.popBack(){
    this->last--;
}
//上面的pop最後一個studnet對象,只是將最後一個元素的指針前移動一個student空間大小,並沒有去析構這個對象,
//而且這個彈出的student對象裡面有一個char *pAddress 指向了外部堆記憶體資源,這個記憶體自己沒有去釋放的話,
//造成了記憶體泄漏了. 這個也是個問題,所以說我們要去析構這個彈出的student對象但是不用使用 delete 方式 delete 這個對象,為什麼呢?
//應為delete 處理調用析構函數完,他還要釋放記憶體空間,但是這個空間屬於Vector的,釋放掉了這一小塊空間,我們後面就無法在使用了,
//**所以一句話就是我們從vector中刪除一個對象時,只做析構這個對象而不釋放他的記憶體空間,把對象的析構和記憶體的釋放分開**

以上三個問題使得我們不能採用上一篇文字中的方式寫Vector,這就是引入了容器的空間配置器的原因

容器的空間配置器做了4件事
記憶體開闢/記憶體釋放 對象創建/對象析構

在上一遍的基礎上加入空間配置器,程式碼如下

#include <iostream>
using namespace std;


class student {

public:

	student(int _age, char * _pname): 
		age(_age),
		pName(_pname){  
	}

	student(const student & _rvalue) {
		this->age = _rvalue.age;
		this->pName = new char[20];
		strcpy(this->pName, _rvalue.pName);
	}

	student & operator =(const student & _rvalue) {
		
		if (this == &_rvalue) { return *this; }
		
		delete[]this->pName;
		this->pName = nullptr;

		this->age = _rvalue.age;	
		this->pName = new char[20];
		strcpy(this->pName, _rvalue.pName);
	}
	~student() {
		delete[] this->pName;
		this->pName == nullptr;
		cout << "student 析構函數被執行,當前對象地址是" << this << endl;
	}


private:
	int age;
	char *pName;
};


template<typename T>
class MyAllocate {

public:

	T * allocate(int size) {
		return (T *)malloc(sizeof(T)*size);
	}

	//根據指定的地址,釋放記憶體空間
	void delAllocate(T *p) {
		free(p);
	}

	//在p指針指定的位置,根據指定的對象創建新對象
	void construct(T * p, const T & _rValue) {
		new (p) T(_rValue);
	}

	//析構指定對象,但不釋放記憶體空間
	void destory(T * p) {
		if (p != nullptr) {
			p->~T();
		}
	}



};



template<typename T, typename Allocate=MyAllocate<T>>
class MyVector2 {

public:

	//構造函數
	MyVector2<T, Allocate>(int size = 10, const Allocate & _allocate = MyAllocate<T>()) : allocator(_allocate) {
		
		first = allocator.allocate(size);
		last = first;
		end = first + size;
		cout << "MyVector2 構造函數,構建數量=" << size <<"堆空間構造起始地址="<<first<<"結束地址=" << endl;
	}

	//拷貝構造,根據指定的 MyVector2 創建新的MyVector2
	MyVector2<T, Allocate>(const MyVector2<T,Allocate> & _rValue) {
		
		//1:根據原MyVector2的Size 申請記憶體空間
		first = allocator.allocate(_rValue.Size());
		last = first;
		end = first + __rValue.Size();

		//2:根據原MyVector2內的對象,在第1步申請的堆記憶體中構造對象
		T *tep = _rValue.first;
		while (tep<_rValue.end)
		{			
			allocate.construct(last, *tep)
			last++;
			tep++;
		}
		cout << "MyVector2 拷貝構造函數,構建數量" << __rValue.Size() << endl;
	}

	//賦值函數
	MyVector2<T, Allocate> & operator=(const MyVector2<T, Allocate> & _rValue) {
		if (this == &_rValue) { return *this;}

		//1:先析構目標Vector2中所有的對象
		T * tep = first;
		while (tep < last) {
			allocator.destory(tep);
			tep++;
		}

		//2:釋放目標Vector2的記憶體空間
		allocator.delAllocate(first);

		//3:根據原MyVector2的size 申請新的記憶體空間
		int rSize = _rValue.Size();
		T * _head allocator.allocate(rSize);
		first = _head;
		last  = _head;
		end   = first + rSize;

		//4:根據原MyVector2中的有效的對象在 第3步申請的記憶體空間上構建對象

		T *tep = _rValue.first;
		while (tep<_rValue.end)
		{
			allocator.construct(last, *tep)
			last++;
			tep++;
		}
	
		cout << "MyVector2 賦值函數,構建數量" << rSize << endl;

	}
	
	//在已經申請空間的位置 添加值
	void pushBack(const T & _addValue) {
		if (Full()) {
			Expend();//兩倍擴容
		}
		//在指定地址空間 構建對象
		allocator.construct(last, _addValue);
		cout << "pushBack 元素,記憶體地址=" << last << endl;
		last++;
		
	}

	//彈出
	void popBack() {
		if (Empty()) { return; }
		//1:只析構指定位置對象,但不釋放空間
		allocator.destory(last - 1);
		cout << "popBack元素,其記憶體地址=" << (last - 1) << endl;

		//2:移動last指針
		last--;
	
	}
	
	//是否為空
	bool Empty() { return first == last; }
	
	//是否滿
	bool Full() { return  last  == end;  }

	int  Size() { return   end - first; }

	//容器擴容
	void Expend() {

		int newSize = this->Size() * 2;//兩倍擴容
		
		//1:申請新的空間
		T * head = allocator.allocate(newSize);
		T * tep_first = head;
		T * tep_last =  head;
		T * tep_end  = first + newSize;

		cout << "兩倍擴容,新的堆記憶體起始地址=" << head << endl;

		//2:將原來有效的對象 賦值到新的堆空間上
		T * tep = first;
		while (tep < last) {
			allocator.construct(tep_first,*tep);
			cout << "兩倍擴容,賦值對象,原對象地址=" << tep <<"目標對象地址="<<tep_first<< endl;
			tep_first++;	
			tep_last++;
			tep++;
		}

		tep = first;
		//3:析構原堆空間上有效的對象
		while (tep < last) {
			allocator.destory(tep);
			cout << "兩倍擴容,析構對象,對象地址=" << tep << endl;
			tep++;
			
		}

		//4:釋放堆上的空間
		allocator.delAllocate(first);
		cout << "兩倍擴容,銷毀原空間" << first << endl;
		first = head;
		last  = tep_last;
		end   = first + newSize;

	}


	void showVectorInfo() {
		T * tep = first;
		while (tep < last)
		{
			cout << "列印Vector中有效對象地址=" << tep << endl;
			tep++;
		}
	}


private:
	T * first;
	T * last;
	T * end;
	Allocate allocator;

};



int main() {

	MyVector2<student, MyAllocate<student>> v(4);

	student s1(20, "zs1");

	v.pushBack(s1);

	student s2(22, "zs2");

	v.pushBack(s2);

	student s3(23, "zs3");

	v.pushBack(s3);


	student s4(24, "zs4");

	v.pushBack(s4);

	v.showVectorInfo();


	student s5(25, "zs5");

	v.pushBack(s5);
	
	v.showVectorInfo();

	system("pause");

	return 1;
}