vector.h

来自「一百个病毒的源代码 包括熊猫烧香等 极其具有研究价值」· C头文件 代码 · 共 100 行

H
100
字号
// Larbin// Sebastien Ailleret// 04-02-00 -> 14-03-00#ifndef VECTOR_HH#define VECTOR_HH#include "types.h"#include <assert.h>template <class T>class Vector { private:  /** This array contain the object */  T **tab;  /** Number of object in the array */  uint pos;  /** Size of the array */  uint size; public:  /** Constructor */  Vector (uint size = StdVectSize);  /** Destructor */  ~Vector ();  /** Re-init this vector : empty it all */  void recycle (uint size = StdVectSize);  /** add an element to this vector */  void addElement (T *elt);  /** give the size of the vector */  inline uint getLength () { return pos; }  /** give the array containing the objects */  inline T **getTab() { return tab; }  /** get an element of this Vector */  T *operator [] (uint i);};/** Constructor * @param size the initial capacity of the Vector */template <class T>Vector<T>::Vector (uint size) {  this->size = size;  pos = 0;  tab = new T*[size];}/** Destructor */template <class T>Vector<T>::~Vector () {  for (uint i=0; i<pos; i++) {	delete tab[i];  }  delete [] tab;}/** Re-init this vector : empty it all */template <class T>void Vector<T>::recycle (uint size) {  for (uint i=0; i<pos; i++) {	delete tab[i];  }  if (this->size > size) {	this->size = size;	delete [] tab;	tab = new T*[size];  }  pos = 0;}/** add an element to this vector */template <class T>void Vector<T>::addElement (T *elt) {  assert (pos <= size);  if (pos == size) {	size *= 2;	T **tmp = new T*[size];	for (uint i=0; i<pos; i++) {	  tmp[i] = tab[i];	}	delete [] tab;	tab = tmp;  }  tab[pos] = elt;  pos++;}/** get an element of this Vector */template <class T>T *Vector<T>::operator [] (uint i) {  if (i<pos) {	return tab[i];  } else {	return NULL;  }}#endif // VECTOR_HH

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?