本文整理汇总了C++中PQueue类的典型用法代码示例。如果您正苦于以下问题:C++ PQueue类的具体用法?C++ PQueue怎么用?C++ PQueue使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PQueue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: fillInColumns
bool Sudoku::fillInColumns() {
PQueue<Row & > queue;
for (int i = 0; i < SUDOKU_SIZE; i++)
queue.push(columns[i]);
return fillInSquares(queue, emptySquare);
}
示例2: fillInBigSquares
bool Sudoku::fillInBigSquares() {
PQueue<BigSquare & > queue;
for (int i = 0; i < SUDOKU_SIZE; i++)
queue.push(squares[i]);
return fillInSquares(queue, emptySquare);
}
示例3: testPQueue
void testPQueue(PQueue& pq){
for(int i = 9 ; i > 0; i--){
pq.add(i);
}
// pq.add(1);
pq.removeAll();
}
示例4: BasicPQueueTest
/*
* Function: BasicPQueueTest
* Usage: BasicQueueTest();
* ------------------------
* Runs a test of the PQueue focusing on simple enqueue, dequeueMax.
* Reports results of test to cout.
*/
void BasicPQueueTest()
{
PQueue pq;
cout << boolalpha; // configure stream to print booleans as true/false instead of 1/0
cout << endl << "----------- Testing Basic PQueue functions -----------" << endl;
cout << "The pqueue was just created. Is it empty? " << pq.isEmpty() << endl;
cout << endl << "Now enqueuing integers from 1 to 10 (increasing order)" << endl;
for (int i = 1; i <= 10; i++)
pq.enqueue(i);
cout << "Pqueue should not be empty. Is it empty? " << pq.isEmpty() << endl;
cout << "Pqueue should have size = 10. What is size? " << pq.size() << endl;
cout << "Dequeuing the top 5 elements: ";
for (int j = 0; j < 5; j++)
cout << pq.dequeueMax() << " ";
cout << endl << "Pqueue should have size = 5. What is size? " << pq.size() << endl;
cout << endl << "Dequeuing all the rest: ";
while (!pq.isEmpty())
cout << pq.dequeueMax() << " ";
cout << endl << "Pqueue should be empty. Is it empty? " << pq.isEmpty() << endl;
}
示例5: PQSort
void PQSort(int array[], int nElems)
{
PQueue pq;
for (int i = 0; i < nElems; i++)
pq.enqueue(array[i]);
for (int i = nElems-1; i >= 0; i--)
array[i] = pq.dequeueMax();
}
示例6: RunPerformanceTrial
/*
* Function: RunPerformanceTrial
* -----------------------------
* Runs a time trial using pqueues of specified size. The first 2 time trials report
* the amount of time necessary to enqueue/dequeue and use pqueue to sort.
* The last trial reports on memory usage.
*/
void RunPerformanceTrial(int size)
{
Randomize();
PQueue pq;
cout << endl << "---- Performance for " << size << "-element pqueue (" << pq.implementationName() << ")" << " -----" << endl << endl;
RunEnqueueDequeueTrial(size);
RunSortTrial(size);
RunMemoryTrial(size);
cout << endl << "------------------- End of trial ---------------------" << endl << endl;
}
示例7: MorePQueueTest
/*
* Function: MoreQueueTest
* Usage: MoreQueueTest();
* -----------------------
* Tests a few more enqueue, dequeueMax, some boundary cases explored.
* Reports results of test to cout.
*/
void MorePQueueTest()
{
PQueue pq;
cout << boolalpha;
cout << endl << "----------- More pqueue testing functions -----------" << endl;
cout << endl << "Enqueuing integers from 15 downto 1 (decreasing order)" << endl;
for (int i = 15; i > 0; i--)
pq.enqueue(i);
cout << "Enqueuing duplicates for even numbers 2 to 14" << endl;
for (int j = 2; j <= 14; j += 2)
pq.enqueue(j);
cout << "Dequeuing the top 10 elements: ";
for (int k = 0; k < 10; k++)
cout << pq.dequeueMax() << " ";
cout << endl << "Dequeuing all the rest: ";
while (!pq.isEmpty())
cout << pq.dequeueMax() << " ";
cout << endl << "Pqueue should be empty. Is it empty? " << pq.isEmpty() << endl;
// The line below calling DequeueMax should cause your program to halt with an error.
// Once you've verified that your PQueue correctly raises this error
// you should comment out the call, so that you can continue on with the other tests.
cout << endl << "This next test raises an error if your pqueue is working correctly." << endl;
cout << "Once you verify the test, comment it out to move on to the other tests." << endl;
cout << "(Comment out line " << __LINE__ + 1 << " in the file " << __FILE__ << ")." << endl;
cout << "Dequeue from empty pqueue returns " << pq.dequeueMax() << endl;
cout << endl << "Hit return to continue: ";
GetLine();
}
示例8: main
int main()
{
PQueue<string> line;
line.Enqueue("A", 3);
line.Enqueue("B", 2);
line.Enqueue("C", 4);
line.Enqueue("D", 2);
line.Enqueue("E", 1);
line.Enqueue("F", 3);
for (int i = 0; i < 6; i++)
{
cout << line.Dequeue() << endl;
}
}
示例9: main
int main() {
PQueue<int> q;
int* a = new int[SIZE];
unsigned int i;
// fill array with random integers
for(i = 0; i < SIZE; i++) {
a[i] = rand()%(SIZE*4);
}
int* p = a;
// insert all items
for( i = 0; i < SIZE; i++) {
//cout << "Inserting: " << *(p+i) << "\n";
q.push(*(p+i));
}
// add all elements to result array
vector<int> result;
while(!q.empty()) {
int * temp = q.top();
q.pop();
result.push_back(*temp);
delete temp;
}
// unsigned int k;
// for (k = 0; k < result.size(); k++) {
// cout << " " << result[k];
// }
// cout << endl;
// If result is not sorted, something went wrong
// uncomment above code to view all elements inline
unsigned int l;
bool fail = false;
for (l = 1; l < result.size() ; l++) {
if (result[l-1] > result[l]) {
cout << "LIST NOT SORTED, FAILURE" << endl;
fail = true;
}
}
if (!fail) {
cout << "Test was successful, integers were sorted" << endl;
}
delete[] a;
return 0;
}
示例10: MorePQueueTest
/*
* Function: MoreQueueTest
* Usage: MoreQueueTest();
* -----------------------
* Tests a few more enqueue, dequeueMax, some boundary cases explored.
* Reports results of test to cout.
*/
void MorePQueueTest()
{
PQueue pq;
cout << boolalpha;
cout << endl << "----------- More pqueue testing functions -----------" << endl;
cout << endl << "Enqueuing integers from 15 downto 1 (decreasing order)" << endl;
for (int i = 15; i > 0; i--)
pq.enqueue(i);
cout << "Enqueuing duplicates for even numbers 2 to 14" << endl;
for (int j = 2; j <= 14; j += 2)
pq.enqueue(j);
cout << "Dequeuing the top 10 elements: ";
for (int k = 0; k < 10; k++)
cout << pq.dequeueMax() << " ";
cout << endl << "Dequeuing all the rest: ";
while (!pq.isEmpty())
cout << pq.dequeueMax() << " ";
cout << endl << "Pqueue should be empty. Is it empty? " << pq.isEmpty() << endl;
}
示例11: RunEnqueueDequeueTrial
/*
* Function: RunEnqueueDequeueTrial
* --------------------------------
* Runs the enqueue & dequeue time trials for the specified
* pqueue size. Reports results to cout.
* Note that we just randomly choose numbers to insert.
* The amount of time it takes to do one enqueue/dequeue is
* too small to be accurately measured, so we do many iterations in
* a loop and time that.
*/
void RunEnqueueDequeueTrial(int size)
{
PQueue pq;
for (int i = 0; i < size; i++)
pq.enqueue(RandomInteger(1,size));
cout << "Time to enqueue into " << size << "-element pqueue: " << flush;
double start = GetCurrentTime();
for (int j = 0; j < NumRepetitions; j++)
pq.enqueue(RandomInteger(1, 2*size));
cout << 1000*(GetCurrentTime() - start)/NumRepetitions << " usecs" << endl;
cout << "Time to dequeue from " << size << "-element pqueue: " << flush;
start = GetCurrentTime();
for (int k = 0; k < NumRepetitions; k++)
pq.dequeueMax();
cout << 1000*(GetCurrentTime() - start)/NumRepetitions << " usecs" << endl;
}
示例12: loadIntoPriorityQueue
void loadIntoPriorityQueue(PQueue &inputPQueue) // Create and add arrival events to event list
{
unsigned int tempTime; //temporary holders for cin input
unsigned int tempLength;
while(cin >> tempTime >> tempLength) //load data into both variables until hits EOF
{
//cout << "Input #" << currentInputCount << ": Time = " << tempTime << ", Length = " << tempLength << endl; //debug
Event tempEvent(tempTime, tempLength);
//cout << "created event successfully" << endl; //debug
inputPQueue.enqueue(tempEvent); //insert our new event into the queue
}
//inputPQueue.print(); //debug
}
示例13: getCharCount
void Encoding::buildEncodingForInput(ibstream& infile){
map<int, int> charMap;
getCharCount(infile, charMap);
PQueue<node*> pq;
map<int, int>::iterator iter;
for (iter = charMap.begin(); iter != charMap.end(); iter++){
node* tempNode = new node;
tempNode->letter = iter->first;
tempNode->weight = iter->second;
tempNode->leftChild = tempNode->rightChild = NULL;
//cout << tempNode->letter << endl;
pq.add(tempNode, iter->second);
}
map <int, string> encodingMap;
while (pq.size() != 1){
node *parent = new node;
parent->letter = 26;
parent->weight = 0; //probably don't need this
parent->leftChild = pq.extractMin();
parent->leftChild->path +='0';
parent->rightChild = pq.extractMin();
parent->rightChild->path += '1';
parent->weight = parent->leftChild->weight+parent->rightChild->weight;
pq.add(parent, parent->weight);
}
//for testing
map<int, int>::iterator it;
node* tree = pq.extractMin();
for (it = charMap.begin(); it != charMap.end(); it++){
BuildEncodingMap(tree, mp[it->first], it->first);
string temps = mp[it->first];
char tempc = it->first;
//cout << tempc << " has the encoding " << temps << endl;
}
//PrintTree(tree);
}
示例14: RunMemoryTrial
/*
* Function: RunMemoryTrial
* ------------------------
* Fills a pqueue to specified size and reports memory usage. Then does
* a bunch of enqueue-dequeue operations to jumble things up and reports
* on the memory usage again. Reports results to cout.
*/
void RunMemoryTrial(int size)
{
PQueue pq;
cout << endl << "Running memory trial on " << size << "-element pqueue" << endl;
for (int i = 0; i < size; i++)
pq.enqueue(RandomInteger(1, size));
cout << "After consecutive enqueues, " << size << "-element pqueue is using "
<< pq.bytesUsed()/1000 << " KB of memory" << endl;
int num = size;
for (int j = 0; j < NumRepetitions; j++) { /* do a bunch of enqueue/dequeue ops */
if (RandomChance(.5)) {
pq.enqueue(RandomInteger(0, size));
num++;
} else {
pq.dequeueMax();
num--;
}
}
cout << "After more enqueue/dequeue, " << num << "-element pqueue is using "
<< pq.bytesUsed()/1000 << " KB of memory" << endl;
}
示例15: main
int main(){
int numItems = 50;
PQueue<int, 100> q; // use PQ tempalate
int i, x;
srand(time(0));
cout << "Enqueuing..." << endl;
for( i = 1; i < numItems; i++) {
x = rand() % 100;
cout << x << " ";
q.insert( x );
}
cout << "\n\nDequeuing..." << endl;
for( i = 1; i < numItems; i++ ) {
x = q.findMin();
q.deleteMin();
cout << x << " ";
}
cout << endl;
assert(q.size() == 0);
q.insert(3);
q.insert(10);
q.insert(9);
x = q.findMin();
q.deleteMin();
cout << x << " ";
x = q.findMin();
q.deleteMin();
cout << x << " ";
q.insert(8);
q.insert(2);
x = q.findMin();
q.deleteMin();
cout << x << " ";
x = q.findMin();
q.deleteMin();
cout << x << " ";
x = q.findMin();
q.deleteMin();
cout << x << " ";
}