function: shiftLeft()
description: shifts all values in the array one space to the left in place (do not allocate a new array)
pre-condition: a MyVector object exists
post-condition: all values are shifted to the left (removes first value from the array)
return: nothing
我正在为动态数组T执行此功能
template <class T>
class MyVector :
{
private:
T *data;
int size;
int capacity;
void grow();
void shiftRight();
void shiftLeft();
public:
MyVector();
}
template <class T>
MyVector<T>:MyVector(){
size = 0;
capacity = 10;
data = new T[capacity];
}
//my function of shiftLeft
//requirement: do not allocate a new array
template <class T>
void MyVector<T>::shiftLeft(){
for(int i = 0; i < size-1; i++){
data[i] = data[i+1];
}
}
我不确定如何清除shiftLeft之后的最后一个元素。
另外,对于函数shiftRight
function: shiftRight()
description: shifts all values in the array one space to the right in place (do not allocate a new array)
pre-condition:a MyVector object exists
post-condition: all values are shifted to the right (opens up one space at the beginning of the array)
return: nothing
我不知道如何在数组的开头打开一个空间