0
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?
       }
    }

誰でもこれを行う方法を説明できますか?

4

2 に答える 2

0

必要に応じて、コンテナをキューに変更できます..

 std::queue<int,std::list<int>> myQueue

このような...

于 2014-04-24T16:58:46.367 に答える