vector subscript out of range怎样解决?
程序代码如下:
//BinomialQueue.h
#ifndef BINOMIALQUEUE_H_H
#define BINOMIALQUEUE_H_H
#include<iostream>
#include<vector>
#include<stdexcept>
using namespace std;
template <typename Comparable>
class BinomialQueue
{
public:
BinomialQueue()
:theTrees(0),currentSize(DEFAULT_TREE) { }
BinomialQueue(const BinomialQueue &rhs)
{
BinomialQueue::operator=(rhs);
}
BinomialQueue(const Comparable &item)
:theTrees(0), currentSize(DEFAULT_TREE)
{
theTrees.push_back(new BinomialNode(item, NULL, NULL));
}
const BinomialQueue &operator=(const BinomialQueue &rhs)
{
if(this == &rhs)
return &this;
makeEmpty();
currentSize = rhs.currentSize;
for(int i = 0; i< rhs.theTrees.size(); ++i)
theTrees[i] = rhs.theTrees[i];
return *this;
}
~BinomialQueue() { makeEmpty(); }
bool isEmpty() const
{
return currentSize == 0;
}
void makeEmpty() const
{
for(int i = 0; i < currentSize; ++i)
makeEmpty( theTrees[i] );
currentSize = 0;
}
const Comparable &findMin() const
{
int minIndex = findMinIndex();
return theTrees[minIndex]->element;
}
void insert(const Comparable &x)
{
BinomialQueue<Comparable> bq(x);
merge(bq);
}
void deleteMin(Comparable &minItem)
{
int minIndex = findMinIndex();
minItem = theTrees[minIndex]->element;
deleteMin();
}
void deleteMin()
{
if(isEmpty())
throw std::underflow_error ("BinomialQueue is empty");
int minIndex = findMinIndex();
BinomialNode * oldRoot = theTrees[minIndex];
BinomialNode * deletedTree = oldRoot->leftChild;
delete oldRoot;
BinomialQueue deletedQueue;
deletedQueue.theTrees.resize(minIndex + 1);
deletedQueue.currentSize = (1 << minIndex) - 1;
for(int j = minIndex - 1; j >= 0; --j)
{
deletedQueue.theTrees[j] = deletedTree;
deletedTree = deletedTree->nextSibling;
deletedQueue.theTrees[j]->nextSibling = NULL;
}
theTrees[minIndex] = NULL;
currentSize -= deletedQueue.currentSize + 1;
merge(deletedQueue);
}
void merge(BinomialQueue &rhs)
{
if( this == &rhs)
return;
currentSize += rhs.currentSize;
if( currentSize > capacity())
{
size_t oldNumTrees = theTrees.size();
size_t newNumTrees = max(theTrees.size(), rhs.theTrees.size()) + 1;
theTrees.resize(newNumTrees);
for(int i = oldNumTrees; i < newNumTrees; ++i)
theTrees[i] = NULL;
}
BinomialNode * carry =NULL;
for(size_t i = 0, j = 1; j < currentSize; ++i, j *= 2)
{
BinomialNode * t1 = theTrees[i];
BinomialNode * t2 = i < rhs.theTrees.size() ? rhs.theTrees[i] : NULL;
int whileCase = t1 == NULL ? 0 : 1;
whileCase += t2 == NULL ? 0 : 2;
whileCase += carry == NULL ? 0 : 4;
switch (whileCase)
{
case 0: /* No Tree */
case 1: /* only this */
break;
case 2: /* only rhs */
theTrees[i] = t2;
rhs.theTrees[i] = NULL;
break;
case 4: /* only carry */
theTrees[i] = carry;
carry = NULL;
break;
case 3: /* this and rhs */
carry = combineTrees(t1, t2);
theTrees[i] = rhs.theTrees[i] = NULL;
break;
case 5: /* this and carry */
carry = combineTrees(t1, carry);
theTrees[i] = NULL;
break;
case 6: /* rhs and carry */
carry = combineTrees(t2, carry);
rhs.theTrees[i] = NULL;
break;
case 7: /* All Tree */
theTrees[i] = carry;
carry = combineTrees(t1, t2);
rhs.theTrees[i] = NULL;
break;
}
}
for(int k = 0; k < rhs.theTrees.size(); ++k)
rhs.theTrees[k] = NULL;
rhs.currentSize = 0;
}
private:
struct BinomialNode
{
Comparable element;
BinomialNode * leftChild;
BinomialNode * nextSibling;
BinomialNode(const Comparable &e, BinomialNode * lt, BinomialNode * sibling)
:element(e), leftChild(lt), nextSibling(sibling) { }
};
enum { DEFAULT_TREE = 1 };
mutable size_t currentSize;
vector<BinomialNode*> theTrees;
int findMinIndex() const
{
int i;
size_t minIndex;
for(i = 0; theTrees[i] == NULL; ++i)
;
for(minIndex = i; i < theTrees.size(); ++i)
if(theTrees[i] != NULL &&
theTrees[i]->element < theTrees[minIndex]->element)
minIndex = i;
return minIndex;
}
int capacity() const
{
return theTrees.capacity();
}
BinomialNode * combineTrees(BinomialNode * t1, BinomialNode * t2)
{
if(t1->element > t2->element)
return combineTrees(t2, t1);
t2->nextSibling = t1->leftChild;
t1->leftChild = t2;
return t1;
}
void makeEmpty(BinomialNode * t) const
{
if(t != NULL)
{
makeEmpty(t->leftChild);
makeEmpty(t->nextSibling);
delete t;
}
t = NULL;
}
BinomialNode * clone(BinomialNode * t) const
{
if(t == NULL)
return NULL;
else
return new BinomialNode(t->element, clone(t->leftChild), clone(t->nextSibling));
}
};
#endif
//Demo.cpp
#include<iostream>
#include "BinomialQueue.h"
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
BinomialQueue<int> bq1;
BinomialQueue<int> bq2;
int t;
srand( (unsigned) time(NULL) );
for(int i = 0; i < 10; ++i)
{
t = rand()%100 + 1;
bq1.insert(t);
}
for(int j = 0; j < 10; ++j)
{
t = rand()%100 + 1;
bq2.insert(j);
}
bq1.merge(bq2);
int i;
while(!bq1.isEmpty())
{
bq1.deleteMin(i);
cout<< i << endl;
}
return 0;
}