C++类模板实现一个STL顺序容器vector容器

         包括:构造函数、析构函数、拷贝构造函数、=运算符重载、push_back() 、pop_back()、返回容器末尾元素back、判断是否满、判断是否空、返回大小、扩容函数expend()。

        vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值。一个容器中的所有对象都必须是同一种类型的。

        vector是一个类模板(class template),使用模板可以编写一个类定义或函数定义,可用于多个不同的数据类型。

//类模板实现一个STL的一个顺序容器 vector向量容器
template<typename T>
class vector
{
public:
	vector(int size = 10)
	{
		_first = new T[size];
		_last = _first;
		_end = _first + size;
	}
	~vector()
	{
		delete[] _first;
		_first = _last = _end = nullptr;
	}
	vector(const vector<T>& rhs)
	{
		int size = rhs._end - rhs._first;
		_first = new T[size];
		int len = rhs._last - rhs._first;
		for (int i = 0; i < len; ++i)
		{
			_first[i] = rhs._first[i];
		}
		_last = _first + len;
		_end = _first + size;
	}
	vector<T>& operator=(const vector<T>& rhs)
	{
		if (this == &rhs)
			return *this;
		delete[]_first;
		int size = rhs._end - rhs._first;
		_first = new T[size];
		int len = rhs._last - rhs._first;
		for (int i = 0; i < len; ++i)
		{
			_first[i] = rhs._first[i];
		}
		_last = _first + len;
		_end = _first + size;
		return *this;
	}
	void push_back(const T& val)  //容器末尾添加元素
	{
		if (full())
			expend();
		*_last++ = val;
	}
	void pop_back() //容器末尾删除元素
	{
		if (empty())
			return;
		--_last;
	}
	T back()const //返回容器末尾的元素值
	{
		return *(_last - 1);
	}

	bool full()const { return _last == _end; }
	bool empty()const { return _first == _last; }
	int size() const { return _last - _first; }
private:
	T* _first; //指向数组起始的位置
	T* _last;  //指向数组中有效元素的后继位置
	T* _end;   //指向数组空间的后继位置

	void expend()
	{
		int size = _end - _first;
		T* ptmp = new T[2 * size];
		for (int i = 0; i < size; ++i)
		{
			ptmp[i] = _first[i];
		}
		delete[]_first;
		_first = ptmp;
		_last = _first + size;
		_end = _first + 2 * size;
	}
};

int main()
{
	vector<int> vec;
	for (int i = 0; i < 20; ++i)
	{
		vec.push_back(rand() % 100);
	}

	while (!vec.empty())
	{
		cout << vec.back() << " ";
		vec.pop_back();
	}
	cout << endl;
	return 0;
}

        除此之外,容器通常都要和空间配置器(allocator)一起使用,所以这个容器还存在很大的问题,不是代码上的问题,它和库里面的容器差了一个“空间配置器”,以下是C++库里面定义的容器的模板参数列表:

    template<class _Ty,
    class _Alloc = allocator<_Ty>>// 默认的模板类型参数

 容器空间配置器的重要性:

        容器通常都要和空间配置器(allocator)一起使用,在上述vector实现的过程中,我们只定义一个容器vec都会导致编译器调用十次(因为定义的默认初始size=10)构造函数和析构函数。这就造成了资源的浪费,而这种情况起因就是:
vector是一个容器,除非不使用了,否则它的空间不应该随便的开辟和释放,然而,
1、每次调使用delete析构,就会既析构了对象,又释放了内存;
2、同样,每次使用new,既构造了对象,又开辟了内存。

        容器空间配置器allocator就是将容器底层内存开辟,内存释放,对象的构造和析构,都通过allocator空间配置器来实现,从而将 内存开辟/内存释放 对象析构/对象构造 进行拆分,是每一步都独立出来。具体的代码如下:

template<typename T>
struct Allocator
{
	T *allocate(size_t size)//内存开辟,size_t?
	{
		return (T*)malloc(sizeof(T) * size);
	}
	void deallocate(void *p)//内存释放
	{
		free(p);
	}
	void construct(T *p, const T &val)//对象构造,在一个已经存在的开辟好的内存上去构造一个值为val的对象
	{
		new (p) T(val);//定位new,在指定的内存P上,构造一个值为val的对象
	}
	void destroy(T *p)//对象析构
	{
		p->~T();//~T()代表了T类型的析构函数
	}

};

/*
容器底层内存开辟,内存释放,对象的构造和析构,都通过allocator空间配置器来实现
*/
template<typename T,typename Alloc = Allocator<T>>
class vector
{
public:
	//vector(int size = 10,const Alloc &alloc = Allocator<T>)//可以传进去,但是也可以不传,就默认使用下面这个Allocator
	//	:_allocator(alloc)									 //template<typename T,typename Alloc = Allocator<T>>
	vector(int size = 10)
	{
		//需求:想要把内存开辟和对象析构分开处理
		//_first = new T[size];//new 一个数组不仅会给它开辟一个空间,还调用构造函数
		_first = _allocator.allocate(size);
		_last = _first;
		_end = _first + size;
	}
	~vector()				//要把对象的析构和内存释放分离开来
	{	//析构容器有效的元素,然后释放整个空间(_first指针指向的堆内存)
		//delete[]_first;		//delete相当于把对象里面的每一个元素都视为有效元素进行析构,
								//但是并不是每一个元素都是有效的,即,end-last段的元素就是无效的
		int size = _last - _first;
		for (int i = 0; i < size; ++i)
		{
			T *p = _first;
			p[i] = _first[i];
			_allocator.destroy(p);
		}
		/*
		for (T *p = _first; p != _last; ++p)
		{
			_allocator.destroy(p);//把_first指针指向的数组的有效元素进行析构操作
		}
		*/
		_allocator.deallocate(_first);//释放堆上的数组内存
		_first = _last = _end = nullptr;
	}
	vector(const vector<T> &rhs)
	{
		int size = rhs._end - rhs._last;
		//_first = new T[size];
		_allocator.allocate(size);
		int len = rhs._last - rhs._first;
		for (int i = 0; i < len; ++i)//这里直接用rhs._last代替len可以吗?
		{
			//_first[i] = rhs._first[i];
			_allocator.construct(_first + i, _first[i]);
		}
		_last = _first + len;
		_end = _first + size;
	}
	vector<T> & operator=(const vector<T> &rhs)
	{
		if (this == &rhs)
			return *this;

		//delete[]_first;
		for (int i = 0; i < size; ++i)
		{
			T *p = _first;
			p[i] = _first[i];
			_allocator.destroy(p);
		}
		/*
		for (T *p = _first; p != _last; ++p)
		{
			_allocator.destroy(p);//把_first指针指向的数组的有效元素进行析构操作
		}
		*/
		_allocator.deallocate(_first);//释放堆上的数组内存

		int size = rhs._end - rhs._last;
		//_first = new T[size];
		_allocator.allocate(size);
		int len = rhs._last - rhs._first;
		for (int i = 0; i < len; ++i)
		{
			//_first[i] = rhs._first[i];
			_allocator.construct(_first + i, _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);//必须进行减一操作,因为_last指向的是有效元素的后继位置
	}
	bool full() const { return _last == _end; }
	bool empty() const { return _last == _first; }
	int size() const { return _last - _first; }
private:
	//T *pvec;		//通常定义的情况
	//int _cur;		//有效元素的个数
	//int _size;	//总长度
	T *_first;				//指向数组的起始位置
	T *_last;				//指向数组有效元素的后继位置
	T *_end;				//指向数组元素的后继位置
	Alloc _allocator;		//定义容器的空间配置器对象

	void expand()	//容器的二倍扩容
	{
		int size = _last - _first;
		//T *tmp = new T[2 * size];			//创建新的空间
		T *tmp = _allocator.allocate(2 * size);
		int len = _last - _first;
		for (int i = 0; i < len; ++i)		//拷贝原来的数据
		{
			//tmp[i] = _first[i];
			_allocator.construct(tmp+i, tmp[i]);
		}
		//delete[]_first;						
		for (int i = 0; i < size; ++i)		//释放原来的空间
		{
			T *p = _first;
			p[i] = _first[i];
			_allocator.destroy(p);
		}
		_allocator.deallocate(_first);//释放堆上的数组内存
		_first = tmp;						//让指针_first指向新的空间
		_last = _first + size;
		_end = _first + 2 * size;					//空间大小变为原来的两倍
	}
};
class Test
{
public:
	Test() { cout << "Test()" << endl; }
	~Test() { cout << "~Test()" << endl; }
	Test(const Test &) { cout << "Test(const Test &)" << endl; }
};
int main()
{
	Test t1, t2, t3;
	cout << "--------------------" << endl;
	vector<Test> vec;//只定义一个空容器时,却打印了10个构造函数和10个析构函数
	vec.push_back(t1);
	vec.push_back(t2);
	vec.push_back(t3);
	cout << "---------------------" << endl;
	vec.pop_back();
	cout << "---------------------" << endl;
	

	return 0;
}

空间配置器就是实现内存开辟/内存释放、 对象析构/对象构造 的分布操作,从而对容器的内存空间进行合理使用。当然,容器的使用也就离不开空间配置器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值