简单实现vector

用operator new实现的Vector模板类,如下:

#include<string>
#include<iostream>
#include<assert.h>
#include<algorithm>
using namespace std;

class Test{
public:
	Test():data(10){
		cout<<"Test()"<<endl;
	}
	Test(const Test& t):data(t.data){
		cout<<"const Test&"<<endl;
	}
	~Test(){
		cout<<"~Test()"<<endl;
	}
	int data;
};

template<typename Type>class Vector{
private:
	Type* ptr;
	typedef unsigned int size_t;
	size_t capacity_;
	size_t size_;
	void re_allocate(){
		size_t cap_tmp=(capacity_==0?1:2*capacity_);
		Type* ptr_tmp=reinterpret_cast<Type*>(::operator new(sizeof(Type)*cap_tmp));
		uninitialized_copy(ptr,ptr+size_,ptr_tmp);
		swap(ptr_tmp,ptr);
		capacity_=cap_tmp;
		for(size_t i=0;i<size_;++i)
			ptr_tmp[i].~Type();
		::operator delete(ptr_tmp);
	}
public:
	Vector():ptr(NULL),capacity_(0),size_(0){}
	Vector(const Vector<Type>& other){
		ptr=reinterpret_cast<Type*>(::operator new(sizeof(Type)*other.capacity_));
		capacity_=other.capacity_;
		size_=other.size_;
		uninitialized_copy(other.ptr,other.ptr+other.size_,ptr);
	}
	~Vector(){
		for(size_t i=0;i<size_;++i)
			ptr[i].~Type();
		::operator delete(ptr);
	}
	Vector<Type>& operator=(const Vector<Type>& other){
		Vector<Type> tmp(other);
		::swap(ptr,tmp.ptr);
		::swap(capacity_,tmp.capacity_);
		::swap(size_,tmp.size_);
		return *this;
	}
	size_t capacity()const{
		return capacity_;
	}
	size_t size()const{
		return size_;
	}
	void push_back(const Type& t){
		if(size_==capacity_)
			re_allocate();
		new(ptr+size_++)Type(t);
	}
	void pop_back(){
		assert(size_!=0);
		ptr[--size_].~Type();
	}
	Type& operator[](size_t idx){
		assert(idx<size_&&idx>=0);
		return ptr[idx];
	}
};


int main(){
	Vector<Test> v1,v2;
	Test t;
	v2.push_back(t);
	
	for(int i=0;i<10;++i)
		v1.push_back(t);
	v1.pop_back();
	
	v1[2].data=20;
	Vector<Test> v3(v1);
	v2=v1;
	
	for(int i=0;i<3;++i)
		cout<<v2[i].data<<endl;
	cout<<v1.size()<<" "<<v2.size()<<endl;	
	return 0;
}


 

allocator类模板的版本

#include<string>
#include<iostream>
#include<memory>
#include<assert.h>
using namespace std;

class Test{
public:
	Test():data(10){
		cout<<"Test()"<<endl;
	}
	Test(const Test& t):data(t.data){
		cout<<"const Test&"<<endl;
	}
	~Test(){
		cout<<"~Test()"<<endl;
	}
	int data;
};

template<typename Type>class Vector{
private:
	typedef unsigned int size_t;
	Type* ptr;
	size_t capacity_;
	size_t size_;
	allocator<Type> allo;
	void re_allocate(){
		size_t cap_tmp=capacity_==0?1:2*capacity_;
		Type* ptr_tmp=allo.allocate(cap_tmp);
		uninitialized_copy(ptr,ptr+size_,ptr_tmp);
		::swap(ptr,ptr_tmp);
		capacity_=cap_tmp;
		for(size_t i=0;i<size_;++i)
			allo.destroy(ptr_tmp+i);
		allo.deallocate(ptr_tmp,size_);
	}
public:
	Vector():ptr(NULL),capacity_(0),size_(0){}
	Vector(const Vector<Type>&other){
		ptr=allo.allocate(other.capacity_);
		uninitialized_copy(other.ptr,other.ptr+other.size_,ptr);
		capacity_=other.capacity_;
		size_=other.size_;
	}
	Vector<Type> operator=(const Vector<Type>&other){
		Vector<Type> tmp(other);
		::swap(ptr,tmp.ptr);
		::swap(capacity_,tmp.capacity_);
		::swap(size_,tmp.size_);
		return *this;
	}
	~Vector(){
		for(size_t i=0;i<size_;++i)
			pop_back();
		allo.deallocate(ptr,capacity_);
	}
	void push_back(const Type& t){
		if(size_==capacity_)
			re_allocate();
		allo.construct(ptr+size_++,t);
	}
	void pop_back(){
		assert(size_!=0);
		allo.destroy(ptr+--size_);
	}
	size_t capacity()const{
		return capacity_;
	}
	size_t size()const{
		return size_;
	}
	Type& operator[](size_t idx){
		assert(idx<size_&&idx>=0);
		return ptr[idx];
	}
};


int main(){
	Vector<Test> v1,v2;
	Test t;
	v2.push_back(t);
	
	for(int i=0;i<10;++i)
		v1.push_back(t);
	v1.pop_back();
	
	v1[2].data=20;
	Vector<Test> v3(v1);
	v2=v1;
	
	for(int i=0;i<3;++i)
		cout<<v2[i].data<<endl;
	cout<<v1.size()<<" "<<v2.size()<<endl;

	return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值