assign1(中文)(5)
发布时间:2021-06-06
发布时间:2021-06-06
作业c++项目
你可以用这些代码来作为你实现的基本代码.
void AddElement( Person& aPerson )
{
// Relocate the array space
if ( size == capacity )
{
Person** temp = _elements;
_elements = new Person*[capacity*2];
for( int i=0; i<size; i++ )
{
_elements [i] = temp[i];
}
capacity*= 2;
delete [] temp;
}
_elements[size++] = &aPerson;
}
Person& RemoveElement()
{
size--;
Person* p = _elements[size];
//shrink the container
if(size < capacity/2)
{
cout << "shrinking\n";
Person** temp = _elements;
_elements = new Person*[capacity/2];
for ( int i=0; i<size; i++ )
{
_elements [i] = temp[i];
}
capacity /= 2;
delete [] temp;
}
return *p;
}
迭代:
NextElement()函数为简单的迭代器提供了一个方法.