首页 > 其他 > 详细

2014金山笔试_编写一个数组类 MyVector

时间:2014-04-17 09:06:14      阅读:543      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
//编写一个数组类 MyVector,数组内容可以动态扩充,实现构造,析构,赋值操作符重载,插入,删除,获取元素个数,获取数组容量(不可以使用STL等的容器类,不能使用
//不连续的存储空间)

1
#include<iostream> 2 using namespace std; 3 class MyVector 4 { 5 public: 6 MyVector() 7 { 8 size=0; 9 val=new int[count]; 10 iterator=val; 11 start=val; 12 end=val; 13 Counts=count; 14 } 15 MyVector(int N) 16 { 17 N=N/count+1; 18 val=new int[count*N]; 19 iterator=val; 20 start=val; 21 end=val; 22 Counts=count*N; 23 } 24 ~MyVector() 25 { 26 delete[] val; 27 } 28 MyVector* & operator=(const MyVector* & vec) 29 { 30 delete val; 31 val=vec->val; 32 size=vec->size; 33 Counts=vec->Counts; 34 start=vec->start; 35 end=vec->end; 36 } 37 void Insert(int num) 38 { 39 size++; 40 if(size>Counts) 41 { 42 Counts=Counts+count; 43 newval=new int[Counts]; 44 for(int i=0;i<size-1;i++) 45 { 46 newval[i]=val[i]; 47 } 48 delete(val); 49 val=newval; 50 start=val; 51 end=val+size-1; 52 } 53 *end=num; 54 end++; 55 } 56 int Delete() 57 { 58 size--; 59 end--; 60 return *end; 61 } 62 int Size() 63 { 64 return size; 65 } 66 int Count() 67 { 68 return Counts; 69 } 70 int *val; 71 int *start; 72 int *end; 73 private: 74 static const int count=10; 75 int Counts; 76 int *iterator; 77 int size; 78 int *newval; 79 }; 80 81 int main() 82 { 83 MyVector* vec=new MyVector; 84 MyVector* vec2=new MyVector; 85 int k; 86 for(int i=1;i<8;i++) 87 { 88 vec->Insert(i); 89 } 90 cout<<"插入前:"; 91 for(int i=0;i<vec->Size();i++) 92 { 93 cout<<vec->val[i]<<" "; 94 } 95 cout<<endl; 96 cout<<"起始值"<<*vec->start; 97 cout<<" ,结束值"<<*(vec->end-1); 98 cout<<" ,数量"<<vec->Size(); 99 cout<<" ,容量"<<vec->Count()<<endl; 100 101 102 vec->Insert(50); 103 cout<<"插入后:"; 104 for(int i=0;i<vec->Size();i++) 105 { 106 cout<<vec->val[i]<<" "; 107 } 108 cout<<endl; 109 cout<<"起始值"<<*vec->start; 110 cout<<" ,结束值"<<*(vec->end-1); 111 cout<<"数量"<<vec->Size(); 112 cout<<"容量"<<vec->Count()<<endl; 113 114 115 116 117 for(int i=100;i<800;i+=100) 118 { 119 vec->Insert(i); 120 } 121 cout<<"插入后:"; 122 for(int i=0;i<vec->Size();i++) 123 { 124 cout<<vec->val[i]<<" "; 125 } 126 cout<<endl; 127 cout<<"起始值"<<*vec->start; 128 cout<<" ,结束值"<<*(vec->end-1); 129 cout<<" ,数量"<<vec->Size(); 130 cout<<" ,容量"<<vec->Count()<<endl; 131 k=vec->Delete(); 132 cout<<"删除一个后:"; 133 cout<<"起始值"<<*vec->start; 134 cout<<" ,结束值"<<*(vec->end-1); 135 cout<<" ,数量"<<vec->Size(); 136 cout<<" ,容量"<<vec->Count(); 137 cout<<" ,删除的元素为:"<<k<<endl; 138 139 vec2=vec; 140 cout<<"给别个赋值后:"; 141 for(int i=0;i<vec2->Size();i++) 142 { 143 cout<<vec2->val[i]<<" "; 144 } 145 cout<<endl; 146 cout<<"起始值"<<*vec2->start; 147 cout<<" ,结束值"<<*(vec2->end-1); 148 cout<<"数量"<<vec2->Size(); 149 cout<<"容量"<<vec2->Count(); 150 system("pause"); 151 return 0; 152 }
bubuko.com,布布扣

bubuko.com,布布扣

2014金山笔试_编写一个数组类 MyVector,布布扣,bubuko.com

2014金山笔试_编写一个数组类 MyVector

原文:http://www.cnblogs.com/xxiaoye/p/3669007.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!