用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;
}