MyVector
来源:互联网 发布:eliza 人工智能 编辑:程序博客网 时间:2024/06/05 16:58
#ifndef _ZB_ARRAY_#define _ZB_ARRAY_#pragma warning (disable : 4996)#include <xmemory>struct ArrayAlloc{ArrayAlloc(){}~ArrayAlloc(){}void* Allocator(size_t len) { return new char[len]; }void Free(void *ptr, size_t len = 0) { delete[] ptr; }template<class Uty>void Destroy(Uty *Ptr){// destroy object at _PtrPtr->~Uty();}template<class Uty>void Destroy(Uty *first, Uty *last){// destroy object at _PtrUty* Ptr = first;while (Ptr){Ptr->~Uty();++Ptr;}}template<class _Objty,class... _Types>void construct(_Objty *_Ptr, _Types&&... _Args){// construct _Objty(_Types...) at _Ptr::new ((void *)_Ptr) _Objty(_STD forward<_Types>(_Args)...);}};template<typename T, class Alloc = ArrayAlloc>class TArray{typedef T value_type;typedef T* printer;typedef T* iterator;typedef T& reference;typedef const T& const_reference;typedef size_t size_type;//typedef TArray<T, Alloc> Myt;public:TArray() :Myfirst(0), Mylast(0), Myend(0) {}TArray(size_type Count){fill_initialize(Count, value_type(0));}TArray(size_type Count, const_reference Val){fill_initialize(Count, Val);}TArray(const TArray<T>& Right){Myfirst = allocate_and_copy();}~TArray(){free_and_fill();}// 返回当前对象个数 size_type size() const { return (this->Mylast - this->Myfirst); }size_type capacity() const { return (this->Myend - this->Myfirst); }size_type max_size() const { return ((size_type)(-1) / sizeof(value_type)); }//查询bool empty() const { return (this->Myfirst == this->Mylast); }reference operator[](size_type off) const { return *(begin() + off); }// 提供访问函数 reference front(){ return (*begin()); }reference back() { return (*(end() - 1)); }// 获取几种迭代器 printer begin() const{ return Myfirst; }printer end() const{ return Mylast; }//插入void insert(iterator Where, size_t Count, value_type& data){//Insert_n(Where, Count, data);}void insert(iterator Where, value_type& data){Insert(Where, data);}void push_back(value_type data){Insert(end(), data);}//删除iterator erase(iterator First, iterator End){iterator Where = copy(End, Mylast, First);// 析构掉需要析构的元素 alloc.Destroy(Where, Mylast);Mylast = Mylast - (End - First);return Mylast;}iterator erase(iterator Where){if (Where + 1 != end())std::copy(Where + 1, Mylast, Where);--Mylast;alloc.Destroy(Mylast);return Where;}void pop_back(){--Mylast;alloc.Destroy(Mylast);}void resize(size_type new_size) { Resize(new_size, value_type()); }void clear() { erase(begin(), end()); }protected:// 这个提供STL标准的allocator接口 typedef std::allocator<value_type> allorator;//typedef simple_alloc <value_type, Alloc> data_allocator;// 释放分配的内存空间 void deallocate(){// 由于使用的是data_allocator进行内存空间的分配, // 所以需要同样使用data_allocator::deallocate()进行释放 // 如果直接释放, 对于data_allocator内部使用内存池的版本 // 就会发生错误 if (Myfirst)alloc.Free(Myfirst, capacity());}// 分配空间, 并且复制对象到分配的空间处 iterator alloc_and_fill(size_type nCount, const_reference Val){iterator res = (value_type*)alloc.Allocator(nCount * sizeof(value_type));std::uninitialized_fill_n(res, nCount, Val);return res;}//释放内存,并置空void free_and_fill(){//析构alloc.Destroy(Myfirst);// 释放内存 deallocate();Myfirst = 0;Mylast = 0;Myend = 0;}iterator Myfirst;iterator Mylast;iterator Myend;private:void fill_initialize(size_type nCount, const_reference Val){_TRY_BEGINMyfirst = alloc_and_fill(nCount, Val);_CATCH_ALLfree_and_fill();_RERAISE;_CATCH_END// 设置当前使用内存空间的结束点 Mylast = Myfirst + nCount;// 构造阶段, 此实作不多分配内存, // 所以要设置内存空间结束点和, 已经使用的内存空间结束点相同 Myend = Mylast;}iterator Insert(iterator Where, value_type Val){size_type Off = Where - begin();if (unused_capacity() > 0)// 剩下的备用空间大于等于“新增元素的个数” {// 在备用空间起始处构造一个元素,并以vector最后一个元素值为其初值 alloc.construct(Mylast, *(Mylast - 1));++Mylast;std::copy_backward(Where, Mylast - 1, Mylast - 1);*Where = Val;}else// 已无备用空间 {const size_type old_size = size();const size_type len = 0 != old_size ? (old_size * 2) : 1;iterator new_first = (value_type*)alloc.Allocator(len * sizeof(value_type));iterator new_last = new_first;// 将内存重新配置 _TRY_BEGIN// 将原vector的安插点以前的内容拷贝到新vector //memmovenew_last = std::uninitialized_copy(Myfirst, Where, new_first);// 为新元素设定初值 x alloc.construct(new_last, Val);// 调整水位 ++new_last;// 将安插点以后的原内容也拷贝过来 new_last = uninitialized_copy(Where, Mylast, new_last);_CATCH_ALL// 回滚操作 alloc.Destroy(new_first, new_last);alloc.Free(new_first, len);_RERAISE;_CATCH_END// 析构并释放原vector alloc.Destroy(new_first, new_last);free_and_fill();// 调整迭代器,指向新vector Myfirst = new_first;Mylast = new_last;Myend = new_first + len;return (begin() + Off);}return end();}// 调整size, 但是并不会重新分配内存空间 void Resize(size_type new_size, const T& x){if (new_size < size())erase(begin() + new_size, end());elseinsert(end(), new_size - size(), x);}size_type unused_capacity() { return (this->Myend - this->Mylast); }private:Alloc alloc;};//创建日期:2016/2/3#endif//_ZB_ARRAY_
STL五个全局函数分别是:construct(),destroy(),uninitialized_copy(),uninitialized_fill(),uninitialized_fill_n()
http://blog.csdn.net/chenhanzhun/article/details/39253027
STL源码剖析---vector
http://blog.csdn.net/hackbuteer1/article/details/7724547
以上参考
编译器:vs2013
加入预处理器(项目属性----C/C++----预处理----预处理器定义):
_SCL_SECURE_NO_WARNINGS
改进
1.在对内存操作进行改进
2.对迭代器进行归为一个类
3.对std::allocator,construct(),destroy(),uninitialized_copy(),uninitialized_fill(),uninitialized_fill_n()学习
0 0
- myVector
- MyVector
- MyVector
- SOJ.MyVector
- SOJ.MyVector
- MyVector案例
- Elements and MyVector
- 9.4:建立一个向量类MyVector
- C++语言基础 例程 案例:MyVector类的设计
- 第九周项目四 我的向量类(MyVector)
- 第九周项目四 我的向量类(MyVector)
- C++找对象的季节(补)——MyVector
- 【STL源码剖析读书笔记】自己实现vector之MyVector
- 数据结构与算法(Java描述)-13、对象数组以及MyVector类、MySet类实现
- 算法学习之路
- git使用
- 机器学习 - 概念学习
- C++模板板块
- Python学习笔记(三)
- MyVector
- ios开发之抽屉效果图
- title: 如何生成多个ssh并将hexo博客布置到github
- Android Resources$NotFoundException异常
- linux下安装apache(httpd-2.4.3版本)各种坑
- 【数位DP】CF55D BZOJ3329 HDU4352 SGU390 HDU5519
- leetcode 7. Reverse Integer
- leetcode 8. String to Integer (atoi)
- Android本地数据安全尝试(中)——Conceal