1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <string>
#include<set>
#include<cmath>
using namespace std;
struct Point
{
string m_name;
double m_x;
double m_y;
Point(string name, double x, double y) : m_name(name), m_x(x), m_y(y) {}
double distance (const Point& p) const
{
return sqrt(pow(m_x - p.m_x,2) + pow(m_y - p.m_y, 2));
}
bool operator < (const Point & p) const
{
Point origin("origin", 0, 0);
return this->distance(origin) < p.distance(origin);
}
};
ostream& operator << (ostream& os, const Point& p)
{
os<<"The name of the point is: "<<p.m_name<<", with co-ordinates: ("<<p.m_x<<", "<<p.m_y<<") \n";
return os;
}
int main(){
set<Point> setPoint;
Point p1("p1", 2, 3);
setPoint.insert(p1);
Point p2 ("p2", 15, 4);
setPoint.insert(p2);
Point p3("p3", -7, 5.6);
setPoint.insert(p3);
Point p4 ("p4", 13, -63.2);
setPoint.insert(p4);
for(auto& itr : setPoint)
{
cout<< itr;
}
}
|