tvector头文件定义tvector类
//tvector.h文件
#include<iostream>
using namespace std;
template<typename t>
class tcontainer
{
virtual void push(const t&) = 0;
virtual void pop()= 0;
virtual const t& begin() = 0;
virtual const t& end() = 0;
virtual size_t size() = 0;
};
template<typename t>
class tvector
{
public:
static const size_t _step = 10;
tvector()
{
_size = 0;
_cap = _step;
buf = 0;
re_capacity(_cap);
}
~tvector()
{
free(buf);
}
void re_capacity(size_t s)
{
if (!buf)
buf = (t*)malloc(sizeof(t)*s);
else
buf = (t*)realloc(buf, sizeof(t)*s);
}
virtual void push(const t& v)
{
if (_size >= _cap)
re_capacity(_cap += _step);
buf[_size++] = v;
}
virtual void pop()
{
if (_size)
--_size;
}
virtual const t& begin()
{
return buf[0];
}
virtual const t& end()
{
if (_size)
return buf[_size-1];
}
virtual size_t size()
{
return _size;
}
const t& operator[](size_t i)
{
if (i >= 0 && i < _size)
return buf[i];
}
private:
size_t _size;
size_t _cap;
t* buf;
};
cpp文件进行简单测试
#include "tvector.h"
using namespace std;
int main()
{
tvector<int> tv;
for (int i = 0; i < 15; ++i)
tv.push(i);
for (int i = 0; i < 15; ++i)
cout << tv[i] << endl;
}
测试结果