理解向量vector的一些实现细节
2017-10-26 10:15
483 查看
#include <iostream> using namespace std; enum ErrorType { invalidArraySize, memoryAllocFail, indexOutOfRange }; const char*ErrorList[]={"invalidArraySize","memoryAllocFail","indexOutOfRange"}; template <class T> class MyVector { public: MyVector(int sz=50); MyVector(const MyVector<T>& V); ~MyVector(); MyVector& operator=(const MyVector& V); T& operator[](int i); operator T*(); // 实现强制类型转换 MyVector---> T*; int Size(); void resize(int sz); void Error(ErrorType err); private: T *Array; int size; }; template<class T> void MyVector<T>::Error(ErrorType err) { cout << ErrorList[err] << endl; exit(1); } template<class T> MyVector<T>::MyVector<T>(int sz) { if(sz<=0) { Error(invalidArraySize); } else { size = sz; Array = new T[size]; if(Array==NULL) Error(memoryAllocFail); } } template<class T> MyVector<T>::MyVector(const MyVector<T>& V) { int n = V.size; size = n; Array = new T ; if(Array==NULL) Error(memoryAllocFail); T*org_array = Array; T*new_array = V.Array; while(n--) *org_array++ = *new_array++; } template<class T> MyVector<T>::~MyVector() { delete [] Array; } template<class T> MyVector<T>& MyVector<T>::operator=(const MyVector<T>& V) { int n = V.size; if(size!=n) { delete[] Array; size = n; Array = new T[size]; if(Array==NULL) Error(memoryAllocFail); } T* org_Array = Array; T* new_Array = V.Array; while(n--) *org_Array++ = *new_Array++; return *this; } template<class T> T& MyVector<T>::operator[](int i) { if(i<0 || i>=size) Error(indexOutOfRange); return Array[i]; } template<class T> MyVector<T>::operator T*() { return Array; } template<class T> void MyVector<T>::resize(int sz) { if(sz<=0) Error(invalidArraySize); if(size == sz) return; int n=(sz<size)?sz:size; T* org_Array = Array; T* new_Array = new T[sz](); if(new_Array==NULL) Error(memoryAllocFail); T* tmp = new_Array; while(n--) *tmp++ =*org_Array++; // *new_Array++ = *org_Array++; // 这句话无法赋值???原因是啥? delete[] Array; Array = new_Array; size = sz; } template<class T> int MyVector<T>::Size() { return size; } int main() { MyVector<int> V1(5); for(int i=0; i<V1.Size(); i++) cin>>V1[i]; MyVector<int> V2 = V1; // 拷贝构造函数,相当于V2(V1); MyVector<int> V3; // 默认构造函数 V3 = V2; // 复制运算 operator= MyVector<int> V4(V3); // 拷贝构造函数; cout << "V2======\n"; for(int i=0; i<V2.Size(); i++) cout << V2[i] << " "; cout << endl; cout << "V3======\n"; for(int i=0; i<V3.Size(); i++) cout << V3[i] << " "; cout << endl; cout << "V4======\n"; for(int i=0; i<V4.Size(); i++) cout << V4[i] << " "; cout << endl; V1.resize(10); cout << "================\n"; for(int *p=V1; p<V1+10; p++) cout << *p << endl; return 0; }
相关文章推荐
- linux 文件IO和标准IO的一些实现细节
- Java中类继承、接口实现的一些细节(长期更新)
- ZOJ 2165题解此题不算难可是有一些细节需注意,很容易出错。总归就是BFS加队列,用STL实现较方便
- Java之Vector向量类实现自动动态增长的对象数组-类似动态数组
- Android面面观——Android事件处理下(按键、触摸屏和滚动球的一些实现细节
- Android事件处理(按键、触摸屏和滚动球的一些实现细节)
- Android面面观——Android事件处理下(按键、触摸屏和滚动球的一些实现细节)
- 用vector实现二维向量
- Servlet中的Request的一些实现细节和应用
- iOS下KVO的使用以及一些实现细节
- sql实现id(key)自增一些细节问题
- 透过一些实例理解数组和指针的一些细节
- 上传文件的一些实现细节
- 词向量、句子向量、篇章向量的一些理解(转)
- Servlet中的Request的一些实现细节和应用
- 《深入理解mybatis原理(十一)》 Mybatis插件原理之实现细节
- Vector3.Lerp 插值的理解(线性),以及Lerp实现匀速运动
- Recyclerview的一些个人理解与使用(四)在界面中实现侧边栏效果
- Android面面观——Android事件处理下(按键、触摸屏和滚动球的一些实现细节)
- 【Android接口实现】ActionBar利用整理的一些细节