my_vectoers.h:
- #ifndef MY_VECTORS_H
- #define MY_VECTORS_H
- #include
-
- using namespace std;
-
- template<typename TYPE>
- class my_vectors
- {
- private:
- TYPE* ptr;
- int num;
- int cnum;
- TYPE* start_ptr=NULL;
- TYPE* end_ptr=NULL;
- public:
- //无参构造
- my_vectors(){};
-
- //有参构造
- my_vectors(int num,const TYPE &val1);
-
- //拷贝构造
- my_vectors(const my_vectors &L);
-
- //构造2
- my_vectors(TYPE* start,TYPE* end);
-
- //=运算符重载
- bool operator=(const my_vectors &L);
-
- //>运算符重载
- bool operator>(const my_vectors &L);
-
- //[]运算符重载
- TYPE & operator[](int index);
-
- //assign函数1
- void assign(TYPE* start,TYPE* end);
-
- //assign函数2
- void assign( int num, const TYPE &val2 );
-
- //at函数
- TYPE at( int pos );
-
- //back函数
- TYPE back();
-
- //伪begin函数
- TYPE* begin();
-
- //capacity 函数
- int capacity();
-
- //clear 函数
- void clear();
-
- //empty 函数
- bool empty();
-
- //full 函数
- bool full();
-
- //伪end函数
- TYPE* end();
-
- //front函数
- TYPE front();
-
- //insert函数1
- TYPE* insert( int loc, const TYPE &val );
-
- //insert函数2
- void insert( int loc, int num, const TYPE &val );
-
- //insert函数3
- void insert( int loc, TYPE* start, TYPE* end );
-
- //max_size 函数
- int max_size();
-
- //pop_back函数
- void pop_back();
-
- //push_back 函数
- void push_back( const TYPE &val );
-
- //伪rbegin 函数
- TYPE* rbegin();
-
- //伪rend 函数
- TYPE* rend();
-
- //resize 函数
- void resize( int size, TYPE val );
-
- //size 函数
- int size();
-
- //swap 函数
- void swap( my_vectors &from );
-
- //三杯水
- void swap(TYPE &a,TYPE &b);
-
- //show函数
- void show();
- };
-
-
- #endif // MY_VECTORS_H
my_vector.cpp:
- #include "my_vectors.h"
-
- //有参构造
- template<typename TYPE>
- my_vectors
::my_vectors(int num,const TYPE &val1) - {
- this->num=num;
- cnum=num;
- ptr=new TYPE(cnum);
- for(int i=0;i
- {
- ptr[i]=val1;
- }
- start_ptr=ptr;
- end_ptr=ptr+num-1;
- }
-
- //拷贝构造
- template<typename TYPE>
- my_vectors
::my_vectors(const my_vectors &L) - {
- this->num=L.num;
- cnum=num;
- ptr=new TYPE(cnum);
- memcpy(this,&L,sizeof(my_vectors));
- memcpy(ptr,L.ptr,sizeof(TYPE)*num);
- start_ptr=ptr;
- end_ptr=ptr+num-1;
- }
-
- //构造2
- template<typename TYPE>
- my_vectors
::my_vectors(TYPE* start,TYPE* end) - {
- num=(end-start)/sizeof(TYPE);
- cnum=num;
- ptr=new TYPE(cnum);
- memcpy(ptr,start,sizeof(TYPE)*num);
- start_ptr=ptr;
- end_ptr=ptr+num-1;
- }
-
- //=运算符重载
- template<typename TYPE>
- bool my_vectors
::operator=(const my_vectors &L) - {
- if(num == L.num && memcmp(ptr,L.ptr,sizeof(num)) == 0)
- {
- return true;
- }
- return false;
- }
-
- //>运算符重载
- template<typename TYPE>
- bool my_vectors
::operator>(const my_vectors &L) - {
- if(num > L.num)
- {
- return true;
- }else if(num == L.num && memcmp(ptr,L.ptr,sizeof(num)) > 0)
- {
- return true;
- }else{
- return false;
- }
- }
-
- //[]运算符重载
- template<typename TYPE>
- TYPE &my_vectors
::operator[](int index) - {
- return *(ptr+index);
- }
-
- //assign函数1
- template<typename TYPE>
- void my_vectors
::assign(TYPE* start,TYPE* end) - {
- delete []ptr;
- num=(end-start)/sizeof(TYPE);
- cnum=num;
- ptr=new TYPE(cnum);
- memcpy(ptr,start,sizeof(TYPE)*num);
- start_ptr=ptr;
- end_ptr=ptr+num-1;
- }
-
- //assign函数2
- template<typename TYPE>
- void my_vectors
::assign( int num, const TYPE &val2 ) - {
- delete []ptr;
- this->num=num;
- cnum=num;
- ptr=new TYPE(cnum);
- for( int i=0;i
- {
- ptr[i]=val2;
- }
- start_ptr=ptr;
- end_ptr=ptr+num-1;
- }
-
- //at函数
- template<typename TYPE>
- TYPE my_vectors
::at( int pos ) - {
- if(pos>=0&&pos
- {
- return ptr[pos];
- }
- else{
- cout<<"pos error";
- }
- }
-
- //back函数
- template<typename TYPE>
- TYPE my_vectors
::back() - {
- return *end_ptr;
- }
-
- //伪begin函数
- template<typename TYPE>
- TYPE* my_vectors
::begin() - {
- return start_ptr;
- }
-
- //capacity 函数
- template<typename TYPE>
- int my_vectors
::capacity() - {
- return cnum;
- }
-
- //clear 函数
- template<typename TYPE>
- void my_vectors
::clear() - {
- while(!empty())
- {
- pop_back();
- }
- }
-
- //empty 函数
- template<typename TYPE>
- bool my_vectors
::empty() - {
- return start_ptr==NULL&&end_ptr==NULL;
- }
-
- //full 函数
- template<typename TYPE>
- bool my_vectors
::full() - {
- return cnum==num;
- }
-
- //伪end函数
- template<typename TYPE>
- TYPE* my_vectors
::end() - {
- return end_ptr;
- }
-
- //front函数
- template<typename TYPE>
- TYPE my_vectors
::front() - {
- return &ptr[0];
- }
-
- //insert函数1
- template<typename TYPE>
- TYPE* my_vectors
::insert( int loc, const TYPE &val ) - {
- if(full())
- {
- cnum*=2;
- TYPE s[num];
- memcpy(s,this,sizeof(TYPE)*num);
- delete []ptr;
- ptr=new TYPE(cnum);
- memcpy(ptr,s,sizeof(TYPE)*num);
- }
- for(int i=ptr+num+1;i>=ptr+loc-1;i--)
- {
- *(ptr+i)=*(ptr+i-1);
- }
- *(ptr+loc-1)=val;
- num++;
- return ptr+loc-1;
- }
-
- //insert函数2
- template<typename TYPE>
- void my_vectors
::insert( int loc, int num, const TYPE &val ) - {
- if(full())
- {
- while(cnum<this->num+num)
- {
- cnum*=2;
- }
- TYPE s[num];
- memcpy(s,this,sizeof(TYPE)*num);
- delete []ptr;
- ptr=new TYPE(cnum);
- memcpy(ptr,s,sizeof(TYPE)*num);
- }
- for(int i=loc-1;i
- {
- *ptr[i]=val;
- }
- num++;
- }
-
- //insert函数3
- template<typename TYPE>
- void my_vectors
::insert( int loc, TYPE* start, TYPE* end ) - {
- int size=(end-start)/sizeof(TYPE);
- if(full())
- {
- while(cnum<this->num+size)
- {
- cnum*=2;
- }
- TYPE s[num];
- memcpy(s,this,sizeof(TYPE)*num);
- delete []ptr;
- ptr=new TYPE(cnum);
- memcpy(ptr,s,sizeof(TYPE)*num);
- }
- TYPE s1[num];
- memcpy(s1,ptr+loc-1,sizeof(TYPE)*(num-loc));
- memcpy(ptr+loc-1,start,sizeof(TYPE)*size);
- memcpy(ptr+loc-1+size,s1,sizeof(TYPE)*(num-loc));
- num++;
- }
-
- //max_size 函数
- template<typename TYPE>
- int my_vectors
::max_size() - {
- return cnum;
- }
-
- //pop_back函数
- template<typename TYPE>
- void my_vectors
::pop_back() - {
- if(!empty())
- {
- num--;
- if(num==0)
- {
- start_ptr=NULL;
- end_ptr=NULL;
- }
- }
- }
-
- //push_back 函数
- template<typename TYPE>
- void my_vectors
::push_back( const TYPE &val ) - {
- if(full())
- {
- cnum*=2;
- TYPE s[num];
- memcpy(s,this,sizeof(TYPE)*num);
- delete []ptr;
- ptr=new TYPE(cnum);
- memcpy(ptr,s,sizeof(TYPE)*num);
- }
- *(ptr+num)=val;
- end_ptr++;
- num++;
- }
-
- //伪rbegin 函数
- template<typename TYPE>
- TYPE* my_vectors
::rbegin() - {
- return end_ptr;
- }
-
- //伪rend 函数
- template<typename TYPE>
- TYPE* my_vectors
::rend() - {
- return start_ptr;
- }
-
- //resize 函数
- template<typename TYPE>
- void my_vectors
::resize( int size, TYPE val ) - {
- delete[] ptr;
- ptr=new TYPE(size);
- num=size;
- cnum=size;
- for(int i=0;i
- {
- *(ptr+i)=val;
- }
- start_ptr=ptr;
- end_ptr=ptr+size-1;
- }
-
- //size 函数
- template<typename TYPE>
- int my_vectors
::size() - {
- return num;
- }
-
- //swap 函数
- template<typename TYPE>
- void my_vectors
::swap( my_vectors &from ) - {
- swap(this->ptr,from.ptr);
- swap(this->num,from.num);
- swap(this->cnum,from.cnum);
- swap(this->start_ptr,from.start_ptr);
- swap(this->end_ptr,from.end_ptr);
- }
-
- //三杯水
- template<typename TYPE>
- void my_vectors
::swap(TYPE &a,TYPE &b) - {
- TYPE temp;
- temp=a;
- a=b;
- b=temp;
- }
-
- //show函数
- template<typename TYPE>
- void my_vectors
::show() - {
- for(int i=0;i
- {
- cout<<*(ptr+i)<<"\t";
- }
- cout<
- }
main.c:
- #include "my_vectors.cpp"
- int main()
- {
- my_vectors<int> a;
- my_vectors<int>b(5,9);
- cout<
empty()< - cout<
full()< - b.show();
- b.pop_back();
- b.pop_back();
- b.push_back(55);
- b.push_back(55);
- b.show();
-
-
相关阅读:
机器学习笔记之指数族分布——最大熵原理与softmax激活函数的关系
S1_servlet与数据库连接、filter过滤器、分页 实操的叙述
敏捷已死?
【ACWing】123. 士兵
uniapp登录逻辑
竞赛选题 深度学习 机器视觉 人脸识别系统 - opencv python
JSP SSM 成果展示系统myeclipse开发mysql数据库springMVC模式java编程计算机网页设计
go.sum文件的作用
VsCode中设置文本格式化(超详细)
脊髓型颈椎病是什么?
-
原文地址:https://blog.csdn.net/wxmchong/article/details/132891656