#include "iostream"
#include "vector"
using namespace std;
template<class T>
class mvector
{
private:
T *p;
unsigned int size;
unsigned int n;
public:
mvector()
{
p=(T*)malloc(10*sizeof(T));
size=10;
n=0;
}
void push_back(T a)
{
if(n==size){p=(T*)realloc(p,10*sizeof(T));size+=10;}
*(p+n)=a;
n++;
}
T& operator[](int d)
{
return *(p+d);
}
//模拟的迭代器
typedef T* iterator;
T* begin()
{
return p;
}
T* end()
{
return p+n;
}
};
int main()
{
mvector<int> vec;
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
mvector<int>::iterator ite;
cout<<vec[0]<<endl;
for(ite=vec.begin();ite!=vec.end();ite++)
cout<<*ite<<endl;
return 1;
}
c++的vector和iterator实现
最新推荐文章于 2024-05-17 20:37:27 发布