#include <iostream>
using namespace std;
template<typename T>
struct Allocator//容器空间配置器 以及SGI STL二级空间适配器内存池原理
{
//负责申请空间----开辟内存空间
T *allocate(size_t size) { 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(); }
};
template<typename T>
class vector
{
public:
vector(size_t size = 10)//构造函数
{
_first = _allocator.allocate(size);//申请size个T类型大小的的空间
_last = _first;
_end = _first + size;
}
~vector()//析构函数
{
T *ptmp = _first;
for (; ptmp != _last; ptmp++)//先释放有效元素的个数(调用它们的析构函数)
_allocator.destroy(ptmp);
_allocator.deallocate(_first);//释放vector在堆上申请的空间
_first = _last = _end = nullptr;
}
vector(const vector<T>&src)//拷贝构造函数
{
size_t size = src._end - src._first;//获取src的数组的总长度
_first = _allocator.allocate(size);//申请size大小的空间
_last = _first;
T *ptmp = src._first;
for (; ptmp != src._last; ptmp++)
_allocator.construct(_last++, *ptmp++);
_end = _first + size;
return *this;
}
vector<T>(const vector<T>&&src)//右值引用
{
_first = src._first;
_last = src._last;
_end = src._end;
return *this;
}
vector<T> operator=(const vector<T>&src)//等号运算符重载
{
if (this == &src)//防止自赋值
return;
~vector();//释放自己原来的资源
//拷贝构造
size_t size = src._end - src._first;//获取src的数组的总长度
_first = _allocator.allocate(size);//申请size大小的空间
_last = _first;
T *ptmp = src._first;
for (; ptmp != src._last; ptmp++)
_allocator.construct(_last++, *ptmp++);
_end = _first + size;
return *this;
}
vector<T> operator=(const vector<T>&&src)//右值引用
{
_first = src._first;
_last = src._last;
_end = src._end;
return *this;
}
void push_back(const T&val)//添加有效元素给数组
{
if (full())
expand();
_allocator.construct(_last++, val);
}
void pop_back()//减少数组有效元素的个数
{
if (empty())
return;
--_last;
_allocator.destroy(_last);
}
T back()//返回数组最后一个有效元素
{
if (empty())
throw "this vector is empty!\n";
return *(_last - 1);
}
bool full()const { return _last == _end; }//是否满了
bool empty()const { return _first == _last; }//是否为空
size_t size()const {return _last - _first; }//数组当前有效元素的个数
T &operator[](int index)
{
if (index < 0 || index >= size())
throw "OutOfRangeException";
return _first[index];
}
/*T operator[](int index)const
{
if (index < 0 || index >= size())
throw "OutOfRangeException";
return _first[index];
}*/
//#1迭代器一般实现成容器的嵌套类型
class iterator {
public:
iterator(T *p = nullptr) :_p(p) {}
bool operator!=(const iterator&it)const
{
return _p != it._p;
}
void operator++()
{
_p++;
}
T& operator*() { return *_p; }
const T& operator*()const { return *_p; }
private:
T *_p;
};
//需要给begin和end方法
iterator begin() { return iterator(_first); }
iterator end() { return iterator(_last); }
private:
T *_first;//指向数组的首元素地址
T *_last;//指向数组最后一个有效元素的后继
T *_end;//指向数组最后一个元素的后继
Allocator<T> _allocator;//容器适配器
void expand()//扩容
{
size_t size = (_end - _first) * 2;
size_t len = _last - _first;
T *ptmp = _allocator.allocate(size);
for (int i = 0; i < len; i++)
{
_allocator.construct(ptmp + i, _first[i]);
_allocator.destroy(&_first[i]);
}
_allocator.deallocate(_first);
_first = ptmp;
_last = _first + len;
_end = _first + size;
}
};
class Test
{
public:
Test() { cout << "test()" << endl; }
~Test() { cout << "~test()" << endl; }
Test(const Test&src) { cout << "test(const test&)" << endl; }
void operator=(const Test&src) = delete;
};
1,使用重载运算符[]来访问
vector<int> vec;
for (int i = 0; i < 20; i++)
vec.push_back(rand() % 100 + 1);
int size = vec.size();
for (int i = 0; i < size; ++i)
cout << vec[i] << " ";
2,使用迭代器方式访问
auto it = vec.begin();
for (; it != vec.end(); ++it)
cout << *it << " ";
cout << endl;
3,使用for each访问
for (int val : vec)
{
cout << val << " ";
}