我们之前有<C++模板编程模块>中的第<四>节 理解空间配置器allocator优化STL中的Vector
我将在此基础上加入迭代器功能代码
Iterator 为什么可以遍历所有的容器的方式都一样?
auto it =continer.beign();
for( ;it!=continer.end();++it){
cout<<*it<<endl;
}
//我们在自己的容器里的 Iterator de ++ , * 由自己来实现,所有对外部使用者来看都是统一的.
//泛型算法能够给所有的容器都使用,也是基于容器对外提供了统一的遍历接口, 其参数接受的都是容器的迭代器
#include <iostream>
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 << endl;
}
//拷贝构造函数
person(const person & _person) {
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 <typename T>
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<T>>
//类模板
class MyVector4 {
public:
MyVector4<T,Allocate>(int size = 4 , const Allocate _rallocator = Allocate4<T>)
: _allocator(_rallocator)
{
pfirst = _allocator.allocator(size);
last = pfirst;
pend = pfirst + size;
cout << "MyVector开辟内存地址=" << pfirst<<endl;
}
MyVector4<T, Allocate>(const MyVector4<T, Allocate> & _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<T, Allocate> & operator=(const MyVector4<T, Allocate> & _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<< endl;
this->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 << endl;
}
bool Empty() const {
return this->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 << endl;
tep++;
}
}
//定义属于自己的迭代器
class Iterator {
public:
Iterator( T * _pAddress=nullptr):pAddress(_pAddress) {
}
const T & operator*() const {
return *pAddress;
}
T & operator*() {
return *pAddress;
}
//前置++
void operator++() {
this->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<person>> v5(4,Allocate4<person>());
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<person>>::Iterator it_begin = v5.begin();
MyVector4<person, Allocate4<person>>::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