#include #include #include using namespace std; // make std accessible class Point2D{ public: Point2D(float x, float y):X_(x),Y_(y){} float getX() const {return X_;} float getY() const {return Y_;} friend ostream &operator<<( ostream &output, const Point2D& p){output << "(" << p.X_ << "," << p.Y_ << ")"; return output;} private: float X_, Y_; }; class LeftRight { // a left-right comparator public: bool operator()(const Point2D& p, const Point2D& q) const { return p.getX() < q.getX(); } }; class BottomTop { // a bottom-top comparator public: bool operator()(const Point2D& p, const Point2D& q) const { return p.getY() < q.getY(); } }; template // element type and comparator void printSmaller(const E& p, const E& q, const C& isLess) { cout << (isLess(p, q) ? p : q) << endl; // print the smaller of p and q } int main(){ priority_queue p1; // a priority queue of integers p1.push(5); p1.push(3); p1.push(7); cout << p1.top() << endl; p1.pop(); cout << p1.top() << endl; p1.pop(); cout << p1.top() << endl; p1.pop(); // a priority queue of points with left-to-right order priority_queue, LeftRight> p2; p2.push( Point2D(8.5, 4.6) ); // add three points to p2 p2.push( Point2D(1.3, 5.7) ); p2.push( Point2D(2.5, 0.6) ); cout << p2.top() << endl; p2.pop(); // output: (8.5, 4.6) cout << p2.top() << endl; p2.pop(); // output: (2.5, 0.6) cout << p2.top() << endl; p2.pop(); // output: (1.3, 5.7) Point2D p(1.3, 5.7), q(2.5, 0.6); // two points LeftRight leftRight; // a left-right comparator BottomTop bottomTop; // a bottom-top comparator printSmaller(p, q, leftRight); // outputs: (1.3, 5.7) printSmaller(p, q, bottomTop); // outputs: (2.5, 0.6) return 0; }