//C++数据结构--01数组二次封装成动态数组 #include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <sstream> using namespace std; template<typename T> class Array { private: T* data; int capacity; int size; public: //无参构造函数,采用模板设置 Array():Array(5){} //构造函数,传入数组的容量 capacity 构造 Array Array(int capacity){ data=new T[capacity];size=0;this->capacity=capacity; } //获取数组中的元素个数 int getsize() { return size; } //获取数组容量 int getCapacity() { return capacity; } //返回数组是否为空 bool isEmpty() { return size==0; } //向所有元素后添加一个新元素 void addLast(T e){ add(size,e); } //在所有元素前添加一个新元素 void addFirst(T e){ add(0,e); } //在第indx个位置上插入一个新元素e void add(int index,T e) { if(size == capacity) resize(2*capacity);//扩容 if(index<0 || index>size) throw "Add failed.Require index>=0 and index<=size."; for(int i=size-1;i>=index;i--) *(data+i+1) = *(data+i); *(data+index)=e; size++; } //获取index索引位置的元素 T get(int index){ if(index<0 || index>=size) throw "Add failed. Index is illegal."; return *(data+index); } //修改index索引位置的元素 void set(int index,T e){ if(index<0 || index>=size) throw "Add failed. Index is illegal."; *(data+index)=e; } //查找数组中是否有元素e bool contains(T e){ for(int i=0;i<size;i++) if(*(data+i)==e) return true; return false; } //查找数组元素第一个e所在的索引,如果不存在e,则返回-1 int find(int e){ for(int i=0;i<size;i++) if(*(data+i)==e) return i; return -1; } //从数组中删除index位置的元素,返回删除的元素 T remove(int index){ if(index<0 || index>=size) throw "Add failed. Index is illegal."; T ret=*(data+index); for(int i=index+1;i<size;i++) *(data+i-1)=*(data+i); size--; if(size==capacity/4 && capacity/2!=0) resize(capacity/2);//缩容 return ret; } //从数组中删除第一个元素,返回删除的元素 T removeFirst() { remove(0); } //从数组中删除最后一个元素,返回删除的元素 T removeLast() { remove(size-1); } //从数组中删除第一个元素e, 没找到返回false bool removeElement(T e){ int index=find(e); if(index) { return remove(index);} else return false; } //重载[] T& operator [](int index) { return data[index]; } //输出所有数组元素 void print(){ for(int i=0;i<size;i++) if(i==0)cout<<"{"<<*(data+i)<<","; else if(i==size-1)cout<<*(data+i)<<"}"<<endl; else cout<<*(data+i)<<","; } private: //动态调整数组大小 void resize(int newCapacity){ T* newData = new T[newCapacity]; for(int i=0;i<size;i++) *(newData+i)=*(data+i); data=newData; capacity=newCapacity; } }; class Student{ private: string name; int score; public: Student(string name,int score){ this->name=name; this->score=score; } Student(){} public: void print(){ printf("Student(name:%s ,score: %d)\n",name.c_str(),score); } //友元函数重载<< friend ostream& operator <<(ostream &os,const Student &s); }; //重载<< ostream& operator <<(ostream &os,const Student &s) { os<<"Student(name:"<<s.name<<" ,score: "<<s.score<<")"; return os; } //重载 && string operator &(const string &s,const int n) { int t=n; string ss; while(t>0){ ss.insert(ss.begin(),t%10+'0'); t/=10; } return s+ss; } //重载 + string operator +(const string &s,int n) { stringstream ss; ss<<n; return s+ss.str(); } void testArry(){ Array<int> aa; for(int i=0;i<10;i++) aa.addLast(i); aa.print(); cout<<aa.get(1)<<endl; aa.add(5,88); aa.set(1,22); aa.print(); cout<<"getsize():"<<aa.getsize()<<endl; cout<<"getCapacity():"<<aa.getCapacity()<<endl; cout<<aa.contains(5)<<endl; cout<<aa.find(8)<<endl; aa.print(); cout<<aa.remove(7)<<endl; aa.print(); cout<<aa.removeFirst()<<" "<<aa.removeLast()<<endl; aa.print(); cout<<aa.removeElement(7)<<endl; aa.print(); cout<<"getsize():"<<aa.getsize()<<endl; cout<<"getCapacity():"<<aa.getCapacity()<<endl; cout<<"isEmpty():"<<aa.isEmpty()<<endl; } int main(int argc, char *argv[]) { try { testArry();} catch(const char* error){ cout<<error<<endl; } /* Student student("Lucy",85); student.print(); Array<Student> arr; arr.addLast(Student("Lily",93)); arr.addLast(Student("Maik",89)); arr.addLast(Student("Jac",77)); for(int i=0;i<arr.getsize();i++) cout<<arr[i]<<" ";cout<<endl; string ss="AAA"; cout<<(ss+"123")<<endl; cout<<(ss&678910456)<<endl; cout<<(ss+678910456)<<endl; */ return 0; }