Last active
September 27, 2016 23:30
-
-
Save bakkiraju/eba445cc9dd2b04cce61f130318fef8f to your computer and use it in GitHub Desktop.
C++ STL playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <map> | |
| #include <queue> | |
| #include <math.h> | |
| using namespace std; | |
| class Point | |
| { | |
| public: | |
| Point() {}; // default constructor | |
| Point(int _x, int _y) { x = _x; y = _y; } // constructor | |
| bool operator<(const Point&) const; // overloaded < operator | |
| int get_x() const { return x; } | |
| int get_y() const { return y; } | |
| private: | |
| int x, y; //data fields | |
| }; | |
| /* overload the less-than operator so priority queues know how to compare two Point objects */ | |
| bool Point::operator<(const Point& p) const | |
| { | |
| return sqrt(pow(x,2)+pow(y,2)) > sqrt(pow(p.x,2)+pow(p.y,2)); | |
| } | |
| int main(int argc, char *argv[]) { | |
| vector<Point> points; | |
| points.push_back(Point(6,2)); | |
| points.push_back(Point(3,4)); | |
| points.push_back(Point(1,1)); | |
| points.push_back(Point(3,2)); | |
| priority_queue<Point> pq; | |
| pq.push(points[0]); | |
| pq.push(points[1]); | |
| pq.push(points[2]); | |
| pq.push(points[3]); | |
| while (!pq.empty()) { | |
| cout << pq.top().get_x() << " " << pq.top().get_y() << endl; | |
| pq.pop(); | |
| } | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Print K closest points from Origin on a cartesian plane