📄 vector.h
字号:
#ifndef VECTOR_H
#define VECTOR_H
#include<stdlib.h>
template<class T>
class vector
{
T** pos;
int pos_sz;
T** neg;
int neg_sz;
int owns;
enum
{
chunk=20,
esz=sizeof(T*),
};
void expand(T**& array,int & size,int index);
public:
vector(int Owns=1);
~vector();
T*& operator[](int index);
int Owns() const{ return owns;}
void Owns(int newOwns){ owns=newOwns;}
};
template<class T>
vector<T>::vector(int Owns):pos(0),pos_sz(0),neg(0),neg_sz(0),owns(Owns){}
template<class T>
vector<T>::~vector()
{
if(owns)
for(int i=0;i<pos_sz;i++)
delete pos[i];
free(pos);
if(owns)
for(int j=0;j<neg_sz;j++)
delete neg[j];
free(neg);
}
template<class T>
T*& vector<T>::operator[](int index)
{
if(index<0)
{
index*=-1;
if(index>=neg_sz)
expand(neg,neg_sz,index);
return neg[index];
}
else
{
if(index>=pos_sz)
expand(pos,pos_sz,index);
return pos[index];
}
}
template<class T> void
vector<T>::expand(T**& array,int &size,int index)
{
const newsize=index+chunk;
const increment=newsize-size;
void* v=realloc(array,newsize*esz);
array=(T**)v;
memset(&array[size],0,increment*esz);
size=index+chunk;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -