unsigned mySize; // number of items I contain
unsigned myCapacity; // how many items I can store
unsigned myFirst; // index of oldest item (if any)
unsigned myLast; // index of next available spot for append (if any)
Item* myArray; // dynamic array of items
我正在创建一个基于动态数组的队列类,并且需要创建一个方法来更改队列可以容纳的项目数。在容量发生变化后,我需要“myLast”保持准确,尤其是在已经有项目从前面出列的队列上。
void ArrayQueue<Item>::setCapacity(unsigned cap) {
if (cap < getSize() || cap == 0){
throw QueueException("setCapacity()", "New capacity must be greater than size");
} else {
Item * nq = new Item[cap];
for (unsigned i = 0; i < cap; i++){
nq[i] = myArray[i];
}
delete [] myArray;
myArray = nq;
myCapacity = cap;
//what do I put here to make myFirst and myLast be correct for the new capcacity?
}
}
谁能解释如何做到这一点?