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

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

2014金山笔试_编写一个数组类 MyVector_第1张图片

采用模板类编程为:

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

2014金山笔试_编写一个数组类 MyVector_第2张图片

val

转载于:https://www.cnblogs.com/xxiaoye/p/3669007.html

你可能感兴趣的:(2014金山笔试_编写一个数组类 MyVector)