MyVector.h内容如下:
#pragma once
template <class T>
class MyVector
{
public:
typedef T value_type;
typedef value_type * iterator;
typedef value_type & reference;
MyVector();
MyVector(int nNum, const T & value);
~MyVector();
iterator Begin(){ return m_ItStart; }
iterator End(){ return m_ItEnd; }
int Size();
int Capacity();
bool Empty();
reference operator[](int nIndex){ return *(m_ItStart + nIndex); }
reference front() { return *(m_ItStart); }
reference back(){ return *(m_ItEnd - 1); }
void PushBack(const T & x);
void PopBack();
void Erase(iterator pos);
void Clear();
private:
iterator m_ItStart;
iterator m_ItEnd;
iterator m_ItStorageEnd;
};
template<class T>
MyVector<T>::MyVector()
{
m_ItStart = NULL;
m_ItEnd = NULL;
m_ItStorageEnd = NULL;
}
template<class T>
MyVector<T>::MyVector(int nNum, const T & value)
{
m_ItStart = (iterator)malloc(sizeof(T)* nNum);
if (m_ItStart)
{
m_ItEnd = m_ItStart + nNum;
m_ItStorageEnd = m_ItStart + nNum;
for (iterator it = m_ItStart; it != m_ItEnd; it++)
new (it) value_type(value);
}
}
template<class T>
MyVector<T>::~MyVector()
{
int nSize = m_ItEnd - m_ItStart;
if (m_ItStart)
{
for (int i = 0; i < nSize; i++)
(m_ItStart + i)->~T();
free(m_ItStart);
}
m_ItStart = NULL;
m_ItEnd = NULL;
m_ItStorageEnd = NULL;
}
template<class T>
int MyVector<T>::Size()
{
return (int)(m_ItEnd - m_ItStart);
}
template<class T>
int MyVector<T>::Capacity()
{
return (int)(m_ItStorageEnd - m_ItStart);
}
template<class T>
bool MyVector<T>::Empty()
{
return (m_ItStart == m_ItEnd);
}
template<class T>
void MyVector<T>::PushBack(const T & x)
{
int nOldSize = 0;
int nNewSize = 0;
if (m_ItEnd < m_ItStorageEnd)
{
new (m_ItEnd) value_type(x);
m_ItEnd++;
return;
}
nOldSize = Size();
if (nOldSize == 0)
nNewSize = 1;
else
nNewSize = 2 * nOldSize;
if (m_ItStart)
m_ItStart = (iterator)realloc(m_ItStart, nNewSize * sizeof(value_type));
else
m_ItStart = (iterator)malloc(nNewSize * sizeof(value_type));
if (m_ItStart)
{
m_ItEnd = m_ItStart + nOldSize;
m_ItStorageEnd = m_ItStart + nNewSize;
new (m_ItEnd) value_type (x);
m_ItEnd++;
}
return;
}
template<class T>
void MyVector<T>::PopBack()
{
if (m_ItEnd > m_ItStart)
{
(m_ItEnd - 1)->~T();
m_ItEnd--;
}
}
template<class T>
void MyVector<T>::Erase(iterator pos)
{
if (pos < m_ItStart)
return;
if (pos >= m_ItEnd)
return;
for (iterator it = pos + 1; it != End(); it++)
{
*(it - 1) = *it;
}
m_ItEnd--;
m_ItEnd->~T();
return;
}
template<class T>
void MyVector<T>::Clear()
{
for (iterator it = m_ItStart; it < m_ItEnd; it++)
{
it->~T();
}
m_ItEnd = m_ItStart;
return;
}
MyVector测试文件内容如下:
// CustomVector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "MyVector.h"
#include <iostream>
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
{
MyVector<int> IntVector;
int i = 0;
for (i = 0; i < 10; i++)
IntVector.PushBack(i + 1);
for (i = 0; i < 10; i++)
cout << IntVector[i] << " ";
cout << endl;
cout << "size:" << IntVector.Size() << endl;
cout << "capacity:" << IntVector.Capacity() << endl;
if (IntVector.Empty())
cout << "empty" << endl;
else
cout << "not empty" << endl;
int nFront = IntVector.front();
int nBack = IntVector.back();
cout << "Front" << nFront << endl;
cout << "Back" << nBack << endl;
IntVector.PopBack();
nBack = IntVector.back();
cout << "new back" << nBack << endl;
for (MyVector<int>::iterator it = IntVector.Begin(); it != IntVector.End(); it++)
if (*it == 5)
IntVector.Erase(it);
for (MyVector<int>::iterator it = IntVector.Begin(); it != IntVector.End(); it++)
cout << *it << " ";
cout << endl;
cout << "size:" << IntVector.Size() << endl;
IntVector.Clear();
cout << "size:" << IntVector.Size() << endl;
cout << "capacity:" << IntVector.Capacity() << endl;
for (i = 0; i < 20; i++)
IntVector.PushBack(i + 1);
for (MyVector<int>::iterator it = IntVector.Begin(); it != IntVector.End(); it++)
cout << *it << " ";
cout << endl;
cout << "size:" << IntVector.Size() << endl;
cout << "capacity:" << IntVector.Capacity() << endl;
if (IntVector.Empty())
cout << "empty" << endl;
else
cout << "not empty" << endl;
nFront = IntVector.front();
nBack = IntVector.back();
cout << "Front" << nFront << endl;
cout << "Back" << nBack << endl;
MyVector<int> IntVector2(10, 1);
i = 0;
for (i = 0; i < 10; i++)
cout << IntVector2[i] << " ";
cout << endl;
cout << "size:" << IntVector2.Size() << endl;
cout << "capacity:" << IntVector2.Capacity() << endl;
if (IntVector2.Empty())
cout << "empty" << endl;
else
cout << "not empty" << endl;
nFront = IntVector2.front();
nBack = IntVector2.back();
cout << "Front" << nFront << endl;
cout << "Back" << nBack << endl;
IntVector2.PopBack();
nBack = IntVector2.back();
cout << "new back" << nBack << endl;
for (MyVector<int>::iterator it = IntVector2.Begin(); it != IntVector2.End(); it++)
if (*it == 5)
IntVector2.Erase(it);
for (MyVector<int>::iterator it = IntVector2.Begin(); it != IntVector2.End(); it++)
cout << *it << " ";
cout << endl;
cout << "size:" << IntVector2.Size() << endl;
IntVector2.Clear();
cout << "size:" << IntVector2.Size() << endl;
cout << "capacity:" << IntVector2.Capacity() << endl;
for (i = 0; i < 20; i++)
IntVector2.PushBack(i + 1);
for (MyVector<int>::iterator it = IntVector2.Begin(); it != IntVector2.End(); it++)
cout << *it << " ";
cout << endl;
cout << "size:" << IntVector2.Size() << endl;
cout << "capacity:" << IntVector2.Capacity() << endl;
if (IntVector2.Empty())
cout << "empty" << endl;
else
cout << "not empty" << endl;
nFront = IntVector2.front();
nBack = IntVector2.back();
cout << "Front" << nFront << endl;
cout << "Back" << nBack << endl;
MyVector<string> StrVector;
for (i = 0; i < 10; i++)
StrVector.PushBack("abc");
for (i = 0; i < 10; i++)
cout << StrVector[i] << " ";
cout << endl;
}
_CrtDumpMemoryLeaks();
return 0;
}