text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

example644.cpp

Go to the documentation of this file.
00001 
00016 #include <iostream>
00017 #include <queue>
00018 #include <vector>
00019 #include <cstdlib>
00020 #include <ctime>
00021 
00022 using namespace std;
00023 
00024 int main()
00025 {
00026    //declare a priority queue
00027    priority_queue<int, vector<int> > pq;
00028 
00029    // declare a vector
00030    vector<int> vheap;
00031 
00032    // declare a vector iterator
00033    vector<int>::iterator iter;
00034 
00035    // seed the random number generator
00036    srand(time(0));
00037 
00038    // fill the priority queue and vector with the random numbers
00039    // push each number of the vector onto a heap
00040    // using the greater predicate
00041    for (int i=0; i < 25; i++)
00042    {
00043       int j = rand() % 25;
00044       pq.push(j);
00045       vheap.push_back(j);
00046       push_heap(vheap.begin(), vheap.end(), greater<int>() );
00047    }  // end for
00048 
00049    // show the generated numbers in their original order by
00050    // iterating through the vector
00051    cout << "Original numbers: " << endl;
00052    for (iter = vheap.begin(); iter != vheap.end(); iter++)
00053       cout << *iter << " ";
00054 
00055    // display the priority queue by popping the top off
00056    cout << endl << "Priority queue: " << endl;
00057    while (!pq.empty())
00058    {
00059       cout << pq.top() << " ";
00060       pq.pop();
00061    }  // end while
00062 
00063    // display the vector as a heap by popping the top off
00064    cout << endl << "Heap: " << endl;
00065    while (!vheap.empty())
00066    {
00067       cout << vheap[0] << " ";
00068       pop_heap(vheap.begin(), vheap.end(), greater<int>());
00069       vheap.pop_back();
00070    }  // end while
00071 
00072    cout << endl;
00073 
00074    return 0;
00075 }  // end main

Generated on Sun Aug 27 22:03:18 2006 for AWLogo by  doxygen 1.4.6