#ifndef _M_VECTOR_H #define _M_VECTOR_H #define DELETEOBJ(p){if(p!=NULL) delete p;p=NULL; }//释放对象 #define DATASIZE 4 //初始缓冲区大小 template<typename T> class m_vevtor { public: m_vevtor() //构造 { this->Data = new T[DATASIZE]; this->Datalen = 0; this->Datasize = DATASIZE; } ~m_vevtor() //析构 { if (Data != NULL) { delete Data; Data = NULL; } } m_vevtor(m_vevtor& other) //深拷贝 { Data = new Data; memcpy(Data, other.Data, other.Datasize*sizeof(T));//拷贝内存字节 this->Datalen = other.Datalen; this->Datasize = other.Datasize; } void push_back(T val) { resize(); Data[Datalen] = val; Datalen++; } void pop_back() //尾删 { int at(T val) const//查找元素第一次出现时的位置 { for (int i = 0; i < Datalen; i++) { if (val == Data[i]) { cout << "查找的元素位置:" << i << endl; return i; } } cout << "未找到要查找的元素" << endl; return -1; } int Size() const//获取存储元素的个数 { return Datalen; } int capacity() const//获取存储空间的大小 { return Datasize; } int& operator[] (int num) const//返回一个元素 { return this->Data[num]; } void printf_array() const //打印 { if (Datalen == 0) { cout << "数据已空" << endl; } else { for (int i = 0; i < Datalen; i++) { cout << Data[i] << endl; } } } int clear() //清空数据 { if (Datalen == 0) { return -1; } else { for (int i = Datalen - 1; i >= 0; i--) { Data[i] == NULL; Datalen--; } } resize(); } void resize() { if (Datalen >= Datasize) //当存储元素个数大于等于空间时自动扩容4个类型大小的空间 { int n = Datasize + 4; //扩容 T* temp = new T[n]; memcpy(temp, Data, n*sizeof(T)); delete Data; Data = temp; Datasize = n; return; } else if (Datasize - DATASIZE >= Datalen &&Datasize-DATASIZE >0) //当减少4个存储空间依然可以存放下数据时,执行该函数减少4个存储空间 { int n = Datasize - 4; T* temp = new T[n]; memcpy(temp, Data, n*sizeof(T)); delete Data; Data = temp; Datasize = n; return; } } private: T* Data; //存放数据的指针 int Datalen; //数据个数 int Datasize;//空间大小 }; #endifView Code
#ifndef _M_VECTOR_H #define _M_VECTOR_H #define DELETEOBJ(p){if(p!=NULL) delete p;p=NULL; }//释放对象 #define DATASIZE 4 //初始缓冲区大小 template<typename T> class m_vevtor { public: m_vevtor() //构造 { this->Data = new T[DATASIZE]; this->Datalen = 0; this->Datasize = DATASIZE; } ~m_vevtor() //析构 { if (Data != NULL) { delete Data; Data = NULL; } } m_vevtor(m_vevtor& other) //深拷贝 { Data = new Data; memcpy(Data, other.Data, other.Datasize*sizeof(T));//拷贝内存字节 this->Datalen = other.Datalen; this->Datasize = other.Datasize; } void push_back(T val) { resize(); Data[Datalen] = val; Datalen++; } void pop_back() //尾删 { int at(T val) const//查找元素第一次出现时的位置 { for (int i = 0; i < Datalen; i++) { if (val == Data[i]) { cout << "查找的元素位置:" << i << endl; return i; } } cout << "未找到要查找的元素" << endl; return -1; } int Size() const//获取存储元素的个数 { return Datalen; } int capacity() const//获取存储空间的大小 { return Datasize; } int& operator[] (int num) const//返回一个元素 { return this->Data[num]; } void printf_array() const //打印 { if (Datalen == 0) { cout << "数据已空" << endl; } else { for (int i = 0; i < Datalen; i++) { cout << Data[i] << endl; } } } int clear() //清空数据 { if (Datalen == 0) { return -1; } else { for (int i = Datalen - 1; i >= 0; i--) { Data[i] == NULL; Datalen--; } } resize(); } void resize() { if (Datalen >= Datasize) //当存储元素个数大于等于空间时自动扩容4个类型大小的空间 { int n = Datasize + 4; //扩容 T* temp = new T[n]; memcpy(temp, Data, n*sizeof(T)); delete Data; Data = temp; Datasize = n; return; } else if (Datasize - DATASIZE >= Datalen &&Datasize-DATASIZE >0) //当减少4个存储空间依然可以存放下数据时,执行该函数减少4个存储空间 { int n = Datasize - 4; T* temp = new T[n]; memcpy(temp, Data, n*sizeof(T)); delete Data; Data = temp; Datasize = n; return; } } private: T* Data; //存放数据的指针 int Datalen; //数据个数 int Datasize;//空间大小 }; #endif
//cpp文件
#include<iostream> #include<windows.h> using namespace std; #include "m_vector.h" int main() { m_vevtor<int> test1; for (int i = 0; i < 9; i++) { test1.push_back(i); //尾插 cout << test1.capacity() << endl; //获取存储空间 } cout << "输出元素:\n"; test1.printf_array(); //打印 cout << "尾删:\n"; test1.pop_back(); //尾删 cout << "查找int类型的值,2在数据中第一次出现后,所在的位置:\n"; cout << test1.at(2) << endl; //查找元素所在的位置 cout << "获取元素的个数:\n"; cout << test1.Size() << endl; //获取元素个数 cout << "获取存储的空间:\n"; cout << test1.capacity() << endl; //获取存储空间 test1.clear();//清空表 test1.printf_array(); //遍历 cout << "------------------------------" << endl; return 0; }