#include<iostream>#include<cstdlib>#include<cstring>usingnamespace std;classElement{private:int number;public:Element():number(0){
cout <<"ctor"<< endl;}Element(int num):number(num){
cout <<"ctor"<< endl;}Element(const Element& e):number(e.number){//复制构造函数
cout <<"copy ctor"<< endl;}Element(Element&& e):number(e.number){//右值引用构造函数
cout <<"right value ctor"<< endl;}~Element(){
cout <<"dtor"<< endl;}voidoperator=(const Element& item){
number = item.number;}booloperator==(const Element& item){return(number == item.number);}voidoperator()(){
cout << number;}intGetNumber(){return number;}};template<typenameT>classVector{private:
T * items;int count;public:Vector():count{0}, items{nullptr}{}Vector(const Vector& vector):count{ vector.count }{
items =static_cast<T*>(malloc(sizeof(T)* count));memcpy(items, vector.items,sizeof(T)* count);}Vector(Vector&& vector):count{ vector.count }, items{ vector.items }{//TODO
vector.count =0;
vector.items =nullptr;}~Vector(){//调用Clear()函数,由于Clear()并不对items析构,且将count置为0,所以不会引发重复析构错误//TODOClear();delete items;}
T&operator[](int index){if(index <0|| index >= count){
cout <<"invalid index"<< endl;return items[0];}return items[index];}intreturnCount(){return count;}voidClear(){//需要调用各个元素的析构函数,但是并不对items析构for(int i =0; i < count; i++){
items[i].~T();}
items =nullptr;
count =0;}voidAdd(const T& item){//类似STL的Vector.push_back(),但是STL中Vector有预存的内存(capacity),不需要每次添加都重新分配//详细可以查看 Vector 的 push_back 实现原理
T *newitems =static_cast<T*>(malloc(sizeof(T)*(count +1)));for(int i =0; i < count; i++){new(&newitems[i])T(move(items[i]));//在新申请的内存构造对象,使用move可以调用对象的右值引用构造函数,提高效率}new(&newitems[count])T(move(item));for(int i =0; i < count; i++){
items[i].~T();//释放原来的对象}
items = newitems;//保存重新申请的内存地址
count++;}boolInsert(const T& item,int index){//插入元素,操作同Add原理if(index <0|| index >count)//判断参数合法性{returnfalse;}
T *newitems =static_cast<T*>(malloc(sizeof(T)*(count +1)));for(int i =0; i < index; i++){new(&newitems[i])T(move(items[i]));}new(&newitems[index])T(move(item));for(int i = index; i < count; i++){new(&newitems[i +1])T(move(items[i]));}for(int i =0; i < count; i++){
items[i].~T();}
items = newitems;
count++;returntrue;}boolRemove(int index){//移除元素,操作同Add原理if(index <0|| index >=count){returnfalse;}
T *newitems =static_cast<T*>(malloc(sizeof(T)*(count -1)));for(int i =0; i < index; i++){new(&newitems[i])T(move(items[i]));}for(int i = index; i < count-1; i++){new(&newitems[i])T(move(items[i+1]));}for(int i =0; i < count; i++){
items[i].~T();}
items = newitems;
count--;returntrue;}intContains(const T& item){//寻找元素,匹配返回索引,不匹配返回-1for(int i =0; i < count; i++){if(items[i]== item){return i;}}return-1;}};template<typenameT>voidPrintVector(Vector<T>& v){//遍历Vector输出int count = v.returnCount();for(int i =0; i < count; i++){
v[i]();
cout <<" ";}
cout << endl;}intmain(){//测试用例
Vector<Element>v;for(int i =0; i <4; i++){
Element e(i);
v.Add(e);}PrintVector(v);
Element e2(4);if(!v.Insert(e2,10)){
v.Insert(e2,2);}PrintVector(v);if(!v.Remove(10)){
v.Remove(2);}PrintVector(v);
Element e3(1),e4(10);
cout << v.Contains(e3)<< endl;
cout << v.Contains(e4)<< endl;
Vector<Element>v2(v);
Vector<Element>v3(move(v2));PrintVector(v3);
v2.Add(e3);PrintVector(v2);return0;}
输出
ctor
copy ctor
dtor
ctor
right value ctor
copy ctor
dtor
dtor
ctor
right value ctor
right value ctor
copy ctor
dtor
dtor
dtor
ctor
right value ctor
right value ctor
right value ctor
copy ctor
dtor
dtor
dtor
dtor
0123
ctor
right value ctor
right value ctor
copy ctor
right value ctor
right value ctor
dtor
dtor
dtor
dtor
01423
right value ctor
right value ctor
right value ctor
right value ctor
dtor
dtor
dtor
dtor
dtor
0123
ctor
ctor
1
-1
0123
copy ctor
1
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor