?? vector.h
字號(hào):
#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
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -