使用c++实现vector容器的功能
使用了空间配置器实现了new和delete功能的拆解,大体实现了vector的功能
#include <iostream>
#include <algorithm>
using namespace std;
template <class T>
//struct 与class只是访问权限的区别 struct都是public
//将new与delete分开 可以尽可能的降低依赖,高内聚也就基本实现(低耦合 高内聚)
struct Allocate{
//1.开辟空间
T* allocate(int size){
T* temp=(T*)malloc(sizeof(T) * size);
if(temp==nullptr){
throw bad_alloc();
}
return temp;
}
//2.构造对象
void conststructor (T* p,const T& obj){
//定位new 在指定的地址上构造对象
//实质是在指定的地址构造一个对象
new(p) T(obj);
}
//3.析构对象
void destructor(T* p){
//手动调用析构
p->~T();
}
//4.释放空间
void destroy(T* p){
free(p);
};
};
template <class T,class Alloc=Allocate<T>>
class Vector{
private:
T* _first;
T* _last;
T* _end;
Alloc _allocater;
public:
Vector(int size=10){
//构造函数中对各个属性赋值 并使用结构体成员开辟空间
//first指向结构体开辟的空间
this->_first=_allocater.allocate(size);
//无成员时 last与first指向相同
this->_last=this->_first;
this->_end=this->_first+size;
}
~Vector(){
if(this->_first != nullptr){
//循环调用结构体内部析构对象逻辑
for(T* p= _first;p!= _last;p++){
_allocater.destructor(p);
}
//调用结构体内部释放空间逻辑
_allocater.destroy(_first);
//重新对内属性赋值
this->_first=this->_last=this->_end=nullptr;
}
}
//拷贝构造函数
Vector(const Vector& other){
//得到新成员的大小
int size=other._end -other._first;
//开辟空间
this->_first=_allocater.allocate(size);
//得到其实际大小
int len=other._last-other._first;
//进行数据移动
memmove(this->_first,other._last,len * sizeof(T));
this->_last = this->_first + len;
this->_end = this->_first + size;
}
//有效元素的个数
int size(){
return this->_last - this->_first;
}
//容器的空间大小
int capacity(){
return this->_end - this->_first;
}
//判空
bool empty(){
return this->_lat == this->_first;
}
//判满
bool full(){
return this->_last == this->_end;
}
//动态扩容的功能
void expand(){
int size =this->_last - this->_first;
T* temp=_allocater.allocate(size * 2);
memmove(temp,this->_first,size * sizeof(T) );
//循环调用结构体内部析构对象逻辑
for(T* p= _first;p!= _last;p++){
_allocater.destructor(p);
}
//调用结构体内部释放空间逻辑
_allocater.destroy(_first);
this->_first = temp;
this->_last = this->_first +size;
this->_end = this->_first + size * 2;
}
//尾部追加数据
void push_back(const T& val){
if(this->full()){
this->expand();
}
_allocater.conststructor(_last,val);
_last++;
}
//尾部删除
void pop_back(){
if(this->empty()){
return ;
}
_last --;
_allocater.destructor(_last);
}
//[] 中括号运算符:
T& operator[](int index)
{
if(index < 0 || index >= this->size())
{
throw out_of_range("越界了");
}
return this->_first[index];
}
//迭代器
class iterator{
private:
T* ptr;
public:
iterator(T* ptr =nullptr){
this->ptr = ptr;
}
//迭代器的!=运算符重载
bool operator!=(const iterator& other){
return this->ptr != other.ptr;
}
//迭代器的 ++运算符重载。
void operator++(){
++ptr;
}
void operator++(int){
ptr++;
}
};
//迭代器接口
iterator begin(){
return iterator(this->_first);
}
iterator end(){
return iterator(this->_last);
}
};
class A
{
public:
A(){
cout << "A的构造" << endl;
}
~A(){
cout << "A的析构" << endl;
}
A(const A& other){
cout << "A的拷贝构造" << endl;
}
};
int main()
{
Vector<int> v;
for(int i = 0; i < 20; i++){
v.push_back(rand()%100 + 1);
}
for(int i = 0; i < v.size(); i++){
cout << v[i] << ",";
}
Vector<A> v1;
A a1, a2 ;
v1.push_back(a1);
v1.push_back(a2);
for_each(v.begin(),v.end(),[](int val){
cout << val << " ";
});
cout << endl;
return 0;
}