在上一个博客中我们用模板类实现了vector向量容器,但是向量容器存在一个问题我们来看看
class Test
{
public:
Test() { cout << "Test" << endl; };
~Test() { cout << "~Test" << endl; };
};
int main()
{
vector<Test> vec1;
return 0;
}
但是输出结果缺如下,
因为new 的时候会自动调用Test的构造函数
所以我们要引入空间配置器目的是把内存开辟和对象构造分开
容器的空间配置器allocator 做四件事 内存开辟 内存释放 对象构造 对象析构
template<typename T>
struct Allocator
{
T* allocate(size_t size)//内存开辟
{
set_new_handler(0);
return (T*)malloc(sizeof(T)*size);
}
void deallocate(void *p)//内存释放
{
free(p);
}
void construct(T *p, const T &val)//负责对象构造
{
new(p) T(val);//定位new
}
void destroy(T *p)//负责对象析构
{
p->~T(); //~T()代表T类型的析构函数
}
};
template<typename T,typename Alloc = Allocator<T>>
class vector
{
public:
vector(int size = 10)
{
//需要把给内存开辟和对象构造分开处理
//_first = new T[size];//new 即使没有赋值 也会开辟空间
_first = _allocator.allocate(size);
_last = _first;
_end = _first + size;
}
~vector()
{
//析构容器的有效元素,然后释放first指针指向的堆内存
//delete[]_first;
for (T * p= _first;p != _last; ++p)
{
_allocator.destroy(p);//把first指针指向的数组的有效元素进行析构操作
}
_allocator.deallocate(_first);//释放堆上的数组内存
_first = _last = _end = nullptr;
}
vector(const vector<T> &swq)
{
int size = swq._end - swq._first;
//_first = new T[size];
_first = _allocator.allocate(size);
int len = swq._last - swq._first;
for (int i = 0;i < len;i++)
{
//_first[i] = swq._first[i];
_allocator.construct(_first + i, swq._first[i]);
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator = (const vector<T> &swq)
{
if (this = &swq)
{
return *this;
}
//delete[]_first;
for (T * p= _first;p != _last; ++p)
{
_allocator.destroy(p);//把first指针指向的数组的有效元素进行析构操作
}
_allocator.deallocate(_first);//释放堆上的数组内存
int size = swq._end - swq._fiest;
//_first = new T[size];
_first = _allocator.allocate(size);
int len = swq._last - swq._first;
for (int i = 0;i < len;i++)
{
//_first[i] = swq._first[i];
_allocator.construct(_first + i, swq._first[i]);
}
_last = _first + len;
_end = _first + size;
return *this;
}
void push_back(const T &val) //向容器末尾添加元素
{
if (full())
expand();
//*_last++ = val; _last指针指向的内存构造一个值为val的对象
_allocator.construct(_last, val);
_last++;
}
void pop_back()//从容器末尾删除元素
{
if (empty())
return;
//--_last; 不仅要把——last指针-- 还需要析构删除的元素
--_last;
_allocator.destroy(_last);
}
T back()const//返回容器末尾元素值
{
return *(_last - 1);
}
bool full()const
{
return _last == _end;
}
bool empty()const
{
return _first == _last;
}
int size()
{
return _end - _first;
}
T& operator[](int index)
{
if (index < 0 || index >= size())
{
throw "out of size";
}
return _first[index];
}
private:
T *_first;//指向数组起始的位置
T *_last; //指向数组中有效元素的后继位置
T *_end; //指向数组空间的后继位置
Alloc _allocator;//定义容器的空间配置器对象
void expand()//容器的二倍扩容接口
{
int size = _end - _first;
//T *ptmp = new T[2 * size];
T *ptmp = _allocator.allocate(2 * size);
for (int i = 0;i < size;i++)
{
_allocator.construct(ptmp + i, _first[i]);
//ptmp[i] = _first[i];
}
//delete[]_first;
for (T *p = _first; p != _last; ++p)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
_first = ptmp;
_last = _first + size;
_end = _first + 2 * size;
}
};
class Test
{
public:
Test() {
cout << "Test()" << endl;
}
~Test() {
cout << " ~Test" << endl;
}
};
int main()
{
Test t1, t2, t3;
cout << "-------------------" << endl;
vector<Test>vec;
vec.push_back(t1);
vec.push_back(t2);
vec.push_back(t3);
cout << "-------------------" << endl;
vec.pop_back();
vector<int>a;
for (int i = 0;i < 20;++i)
{
a.push_back(rand() % 100 + 1);
}
vector<int>::iterator it = a.begin();
for (;it != a.end();++it)
{
cout << *it << " ";
}
return 0;
}
现在就好了