#include #include #include #include // for greater #include using namespace std; // "Non-destructive" print function? template void print_queue(T q) { while(!q.empty()) { std::cout << q.top() << " "; q.pop(); } std::cout << '\n'; } // a binary predicate (function object/functor) for comparing two strings // returns true if first string is shorter than second string struct longer { bool operator()(const string& a, const string& b) { return a.size() < b.size(); } }; int main () { int myints[]= {10,60,50,20}; vector v1{10,20,30,40}; vector v2{"Have","a","really","very","nice","day","."}; // pq1, pq2, pq3 uses default < comparison for type int priority_queue pq1; priority_queue pq2 (v1.begin(), v1.end()); priority_queue pq3 (myints,myints+4); // pq4 uses default > comparison for type int to determine priority priority_queue, std::greater > pq4 (myints,myints+4); // pq5 uses default < comparison for type string priority_queue pq5 (v2.begin(),v2.end()); // pq6 uses longer binary predicate comparison for type string to determine priority priority_queue, longer> pq6 (v2.begin(),v2.end()); cout << "pq2 = "; print_queue(pq2); cout << "pq3 = "; print_queue(pq3); cout << "pq4 = "; print_queue(pq4); cout << "pq5 = "; print_queue(pq5); cout << "pq6 = "; print_queue(pq6); cout << "pq3.size()=" << pq3.size() << endl; cout << "pq4.size()=" << pq4.size() << endl << endl; cout << "pq2 and pq3 swapped" << endl; pq2.swap(pq3); // pq3.swap(pq4); ERROR - why? cout << "pq2 = "; print_queue(pq2); pq2.push(95); pq2.push(5); pq2.push(25); pq2.emplace(35); cout << "pq2 = "; print_queue(pq2); }