我们之前有中的第节 理解空间配置器allocator优化STL中的Vector
我将在此基础上加入迭代器功能代码
Iterator 为什么可以遍历所有的容器的方式都一样?auto it =continer.beign();for( ;it!=continer.end();++it){ cout<<*it<<endl;}//我们在自己的容器里的 Iterator de ++ , * 由自己来实现,所有对外部使用者来看都是统一的.//泛型算法能够给所有的容器都使用,也是基于容器对外提供了统一的遍历接口, 其参数接受的都是容器的迭代器
#include using namespace std;class person {public://构造函数person(int _age=1,char * _pname=nullptr):age(_age){if (_pname == nullptr) {pname = new char[1];pname[0] = '\0';}else {int size = strlen(_pname);pname = new char[size + 1];strcpy(pname, _pname);}cout << "创建student对象,地址=" << this <age = _person.age;int size = strlen(_person.pname);pname = new char[size + 1];strcpy(this->pname, _person.pname);}//赋值函数person & operator=(const person & _person) {if (this == &_person) { return *this; }delete[]pname;pname = nullptr;this->age = _person.age;int size = strlen(_person.pname);pname = new char[size + 1];strcpy(this->pname, _person.pname);return *this;}~person() {cout << "析构 person =" <<pname << " "<<age << endl;delete[]pname;pname = nullptr;}private:int age;char * pname;friend ostream & operator<<(ostream & out, const person & _value);};ostream & operator<<(ostream & out, const person & _value) {cout << _value.pname<<" == "<< _value.age << " " << endl;return out;}template class Allocate4 {public://分配内存空间,不创建对象T * allocator(int size=4) {return (T *)malloc(sizeof(T)*size);}//在指定的内存空间地址,构建 T对象void constract(T * pAddress, const T & _val) {new (pAddress) T(_val);}//释放指定位置的内存空间void delAllocator(T * pAddress) {if (pAddress != nullptr) {free(pAddress);pAddress = nullptr;}}//析构指定内存位置void destory(T * pAddress) {pAddress->~T();//调用析构函数}};template<typename T,typename Allocate= Allocate4>//类模板class MyVector4 {public:MyVector4(int size = 4 , const Allocate _rallocator = Allocate4): _allocator(_rallocator){pfirst = _allocator.allocator(size);last = pfirst;pend = pfirst + size;cout << "MyVector开辟内存地址=" << pfirst<<endl;}MyVector4(const MyVector4 & _vector){//1:根据原vector的空间大小申请新的内存空间pfirst = _allocator.allocator(_vector.size());last = pfirst;pend = pfirst + size;//2:将原vector空间中的有效对象赋值到新的vector中T * _pFlag = _vector.pfirst;while (_pFlag != _vector.last) {_allocator.constract(last, *_pFlag);_pFlag++;last++;}}MyVector4 & operator=(const MyVector4 & _vector){if (this == &_vector) {return *this;}//1:析构现有vector中的有效对象T * _pFlag = pfirst;while (_pFlag !=last) {_allocator.destory(_pFlag);_pFlag++;}//2:释放现有的vector申请的堆内存空间_allocator.delAllocator(pfirst);pfirst = nullptr;pend = nullptr;last = nullptr;//3:根据_vector的内存空间大小,申请新的堆内存空间pfirst = _allocator.allocator(_vector.size());last = pfirst;pend = pfirst + _vector.size();//4:将_vector中有效的对象复制到现在新的堆空间中T * _pFlag = _vector.pfirst;while (_pFlag != _vector.last) {_allocator.constract(last, *_pFlag);_pFlag++;last++;}}void pushBack(const T & _val) {if (Full()) {Expend();}_allocator.constract(last, _val);cout << "pushBack 原对象 地址=" << &_val <<"放在内存位置 "<<last<last++;}void popBack() {if (Empty()) { return ; }_allocator.destory(this->last-1);this->last--;}//内存空间扩展void Expend() {int newSize = 2*this->size();//1:申请新的内存空间T * tep_pfirst = _allocator.allocator(newSize);T * tep_last = tep_pfirst;T * tep_pend = tep_pfirst + newSize;//2:原当前vector中原有效的对象复制到新的堆空间上T * _pFlag = pfirst;while (_pFlag != last) {_allocator.constract(tep_last, *_pFlag);_pFlag++;tep_last++;}//3:析构原有对象_pFlag = pfirst;while (_pFlag != last) {_allocator.destory(_pFlag);_pFlag++;}//4:释放原有的vector申请的堆内存空间_allocator.delAllocator(pfirst);pfirst = nullptr;pend = nullptr;last = nullptr;//5:指针重新指向pfirst = tep_pfirst;last = tep_last;pend = tep_pend;cout << "MyVector空间2倍扩展,新的地址=" << pfirst <pfirst == this->last;}bool Full() const {return this->pend == this->last;}int size() {return this->pend - this->pfirst;}void showVectorInfo() {T * tep = pfirst;while (tep < last){cout << "打印Vector中有效对象地址=" << tep <pAddress++;}bool operator!=(const Iterator & src) const {return this->pAddress != src.pAddress;}private:T * pAddress;};Iterator begin() const {return Iterator(this->pfirst);}Iterator end() const {return Iterator(this->last);}private:T * pfirst;//指向首元素地址T * pend; // 指向容器最后位置的下一个地址T * last; //指向最后一个有效元素的下一个位置Allocate _allocator;};void testV4() {MyVector4<person, Allocate4> v5(4,Allocate4());person p1(10,"zs1");v5.pushBack(p1);person p2(20, "zs2");v5.pushBack(p2);person p3(30, "zs3");v5.pushBack(p3);person p4(40, "zs4");v5.pushBack(p4);v5.showVectorInfo();cout << "-------" << endl;person p5(50, "zs5");v5.pushBack(p5);v5.showVectorInfo();v5.popBack();MyVector4<person, Allocate4>::Iterator it_begin = v5.begin();MyVector4<person, Allocate4>::Iterator it_end = v5.end();cout << "---iterator begin----" << endl;for (; it_begin != it_end; ++it_begin) {cout << *it_begin << endl;}cout << "---iterator end----" << endl;}int main() {testV4();system("pause");return 0;}//pendl