// test_priority_queue.cpp just demo some features, not a full test #include #include using namespace std; typedef priority_queue my_queue; // give my typename to a priority queue of integers // this simplifies code for function prototypes, etc. void put(const my_queue Q) // function to output a priority queue // of type my_queue (no iterator available) { my_queue T = Q; // make a copy because we have to pop to get next highest priority cout << "Q(" << Q.size() << ")= "; // use while loop to be safe while(!T.empty()) { cout << T.top() << " "; T.pop(); } cout << endl; } int main() { my_queue Q; // construct an empty queue cout << "test_priority_queue running, Q is " << (Q.empty() ? "empty" : "has junk") << endl; Q.push(3); Q.push(5); Q.push(1); Q.push(4); Q.push(2); // push in any order, default comes out largest first cout << " initial "; put(Q); // size() and top() demonstrated in 'put()' Q.pop(); // removes highest priority item (largest by default) Q.push(3); // duplicates OK Q.push(3); cout << " added 3's "; put(Q); return 0; } // Output of this program is: // test_priority_queue running, Q is empty // initial Q(5)= 5 4 3 2 1 // added 3's Q(6)= 4 3 3 3 2 1