已经进入期末复习了,有一周没写过程序了,感觉还是写点什么比较好,于是想实现一下vetor,但是写了一部分,在网上看到说vs2008对stantard library 做了extension,查看里面的源码发现有
unchecked_uninitialized_fill_n这么一个函数,觉得这个肯定是扩展了,从而my code implemented using this algorithm will not be portable.于是看来看去不知道用那一个配置器了?还是看点别的吧,下面是今天写的部分代码,留着以后想起来了试着补充一下
/*------------------------------------------------------------------- * Purpose: * myvetor.h//尝试运用配置器与迭代器 * Time: * 2011年1月6日 21:03:08 * Author: * 张彦升 --------------------------------------------------------------------*/ #ifndef _MYVECTOR_H #define _MYVECTOR_H #include <memory> #include <stdexcept> namespace lisency { template <class _Elem, class Allocator = std::allocator<_Elem> > class vector { public: typedef _Elem value_type; typedef value_type* pointer; typedef value_type* iterator; typedef value_type& reference; typedef size_t size_type; typedef ptrdiff_t difference_type; protected: typedef Allocator data_allocator; iterator start; iterator finish; iterator end_of_storage; void _Destroy(pointer _First, pointer _Last) { // destroy [_First, _Last) using allocator _Destroy_range(_First,_Last,this->data_allocator); } void fill_initialize(size_type n,const value_type& value) { start = allocate_and_fill(n,value); finish = start + n; end_of_storage = finish; } iterator allocate_and_fill(size_type n,const value_type& value) { iterator result = data_allocator::allocator(n); uninitialized_fill_n(result,n,value); return result; } void deallcatoe() { if (begin()) { data_allocator::deallocate(begin(),capacity()); } } insert_aux(iterator position,value_type value) { } public: vector(size_type n,const value_type& value) { } vector(int n,const value_type& value) { } vector(long n,const value_type& value) { } explicit vector(size_type n) { } ~vector() { if (start != 0) { // something to free, destroy and deallocate it _Destroy(begin(),end()); this->data_allocator.deallocate(begin(),begin() - end()); } start = 0, finish = 0, end_of_storage = 0; } iterator begin() { return start; } iterator end() { return finish; }//这里没有必要专门用一个函数得知这个向量的可用空间的尾 size_type size() { return size_type(end() - begin()); } size_type capacity()const { return size_type(end_of_storage - begin()); } bool empty() { return (begin() == end()); } reference front() { return *begin();//要返回引用类型,这样可以吗? } reference back() { return *end(); } void push_back(const value_type* value) {//将元素插到末尾 if (size() < capacity()) { data_allocator.construct(end(),value); ++finish; } else { insert_aux(end(),value); } } void pop_back() { --finish; data_allocator.destroy(finish); } iterator erase(iterator position) { if (position + 1 = end()) { copy(position + 1,finish,position); --finish; data_allocator.destroy(finish); return position; } } void resize(size_type new_size,const value_type& value) { if (new_size < size()) { erase(begin() + new_size,end()); } else { //insert_aux() } } }; } #endif //_MYVECTOR_H