• 简易vector的实现


    在这里我们实现了一个简易的vector,没有利用到 stl中的内存分配器,内存分配利用的是new进行分配。其余功能大致实现 1 #ifndef _NVECTOR_

     2 #define _NVECTOR_
     3 #include<cstddef>
     4 #include <algorithm>
     5 template<typename T>
     6 class nvector{
     7 public:
     8     typedef T value_type;
     9     typedef value_type* pointer;
    10     typedef value_type& reference;
    11     typedef value_type* iterator;
    12     typedef size_t size_type;
    13     typedef ptrdiff_t difference_type;
    14 private:
    15     iterator start; //迭代器的起始位置
    16     iterator finish;
    17     iterator end_of_storage;
    18     void insert_aux(iterator position, const T& value);
    19 public:
    20     iterator begin(){return start;}
    21     iterator end(){return finish;}
    22     size_type size(){return size_type(end()-begin());}
    23     size_type capacity(){return size_type(end_of_storage-begin());}
    24     bool empty(){return start==finish;}
    25     reference operator [](size_type index){return *(begin()+index);}
    26     nvector():start(0),finish(0),end_of_storage(0){}
    27     nvector(size_type n, const T& value){
    28         start = new T[n+1];
    29         for(int i=0;i<n;i++)
    30             start[i] = value;
    31         finish = start+n;
    32         end_of_storage = start+n;
    33     }
    34     explicit nvector(size_type n){start = new T[n+1];finish = start; end_of_storage = start+n;}
    35     ~nvector(){if(start!=NULL) delete[] start;finish=0;end_of_storage = 0;}
    36     reference front(){return *begin();}
    37     reference back(){return *(end()-1);}
    38     void push_back(const T& value){
    39         if(finish!=end_of_storage){
    40             *finish = value;
    41             ++finish;
    42         }else
    43             insert_aux(finish, value);
    44     }
    45     void pop_back(){
    46         if(!empty()){
             *finish->~T();
    47 --finish;
          }
    48 } 49 void clear(){ 50 finish = start; 51 } 52 }; 53 54 template<typename T> 55 void nvector<T>::insert_aux(iterator position,const T& value){ 56 if(finish!=end_of_storage){ 57 T x_copy = value; 58 copy_backward(position,finish,finish+1);
           finish++;
    59 *position = x_copy; 60 }else{ 61 const size_type old_size = size(); 62 const size_type new_size = old_size!=0?2*old_size:1; 63 iterator new_start = new T[new_size]; 64 iterator new_finish = copy(start,position,new_start); 65 T x_copy = value; 66 *new_finish = x_copy; 67 ++new_finish; 68 new_finish = copy(position,finish,new_finish); 69 if(start!=NULL) 70 delete[] start; 71 start = new_start; 72 finish = new_finish; 73 end_of_storage = start+new_size; 74 } 75 } 76 77 #endif
  • 相关阅读:
    oracle:数据库对象:创建用户和赋予权限,数据表,序列,事务,约束
    oracle:多表关联 join on,单行子查询,多行子查询,TOP-N,行转列
    oracle:数值型函数,日期函数,转换函数,decode/case when条件判断,组函数,分组group by,排序order by,两表查询连接
    informix建临时表索引
    jbpm4.3表结构和表字段说明
    JBPM4 常用表结构及其说明
    Docker技术学习
    千万PV级别WEB站点架构设计
    四层和七层负载均衡的区别
    STORM在线业务实践-集群空闲CPU飙高问题排查
  • 原文地址:https://www.cnblogs.com/zhang-wen/p/4779711.html
Copyright © 2020-2023  润新知