# define MYVECTOR_H
# include & lt; Iostream>
# include & lt; Cstring>
Using namespace STD.
//custom container class
Template
The class MyVector
{
Public:
MyVector ();
MyVector (int len, T element);//fill len length of element element
MyVector (const MyVector
Virtual ~ MyVector ();
Template
MyVector
Protected:
Private:
T * m_elements;//used to store elements of the array
Int m_length;//all the storage elements, the actual number of
Int m_capacity;//the size of the current element array
};
Template
MyVector
{
This - & gt; M_elements=new T [m_capacity];
}
Template
MyVector
{
M_capacity=len + m_capacity;
M_length=len;
M_elements=new T [m_capacity];
for (int i=0; I & lt; M_length; I++)
{
Memcpy (& amp; M_elements [I], & amp; Element, sizeof (T));
}
}
Template
MyVector
{
If (this==& amp; Vec)
Return * this;
If (this - & gt; M_elements!=nullptr)
{
The delete [] this - & gt; M_elements;
This - & gt; M_elements=nullptr;
This - & gt; M_capacity=16;
This - & gt; M_length=0;
}
This - & gt; M_capacity=vec. M_capacity;
This - & gt; M_length=vec. M_length;
This - & gt; M_elements=new T [vec m_capacity];
Memcpy (m_elements, vec. M_elements m_length * sizeof (T));
}
Template
MyVector
{
This - & gt; M_capacity=vec. M_capacity;
This - & gt; M_length=vec. M_length;
This - & gt; M_elements=new T [vec m_capacity];
Memcpy (m_elements, vec. M_elements m_length * sizeof (T));
}
Template
T& MyVector
{
Return m_elements [index];
}
Template
Void MyVector
{
If (nullptr==m_elements)
{
M_capacity=16;
M_length=0;
M_elements=new T [m_capacity];
}
//determine whether the current array is full
If (m_length==m_capacity)
{
//if full, expansion: * 2 + 1, the current capacity expansion and can also be a fixed value, according to the actual demand
T * newElement=new T [m_capacity * 2 + 1);
//copy the elements of the original into the new space in
Memcpy (newElement m_elements, m_length * sizeof (T));
The delete [] m_elements;
M_elements=newElement;
}
Memcpy (& amp; M_elements [m_length + +], & amp; Element, sizeof (T));
}
Template
for (int i=0; I & lt; Vec. M_length; I++)
{
The out & lt;
The out & lt;
}
Template
MyVector
{
The delete [] m_elements;
}
# endif//MYVECTOR_H
//the main CPP
# include & lt; Iostream>
#include
# include "MyVector. H"
Using namespace STD.
Void TestVector ();
Int main ()
{
TestVector ();
return 0;
}
Void TestVector ()
{
MyVector
Cout & lt; <"Vec1:" & lt;
Cout & lt; <"Vec2:" & lt;
//cout & lt; <"Vec3:" & lt;
//vec3) push_back (" GHK ");
//cout & lt; <"After insert element \ n" & lt; <"Vec3:" & lt;
MyVector
Cout & lt; <"Vec4:" & lt;
Cout & lt; <"Vec5:" & lt;
Cout & lt; <"Equal assignment vec5=vec2 vec5:" & lt;