Instantly share code, notes, and snippets.

# learning2learn/gist:4099484 Created Nov 17, 2012

 #include #include #include #include #include #include #include #include using namespace std; class Point2D{ protected: int x,y; double distFrOrigin; void setDistFrOrigin(); public: Point2D(){}; Point2D(int x, int y); int getX(); int getY(); double getScalarValue(); void setX(int x); void setY(int y); virtual string toString(); Point2D operator - (Point2D); bool operator == (Point2D); bool operator < (Point2D); }; Point2D::Point2D(int x, int y){ setX(x); setY(y); setDistFrOrigin(); } void Point2D::setX(int x) { this->x = x; } void Point2D::setY(int y) { this->y = y; } void Point2D::setDistFrOrigin() { double compute=0.000f; compute = sqrt(pow(x,2) + pow(y,2)); distFrOrigin = compute; } int Point2D::getX() { return x; } int Point2D::getY() { return y; } double Point2D::getScalarValue() { setDistFrOrigin(); return distFrOrigin; } string Point2D::toString(){ stringstream ss; ss << "["; ss.width(4); ss << right << x; ss << ", "; ss.width(4); ss << right << y; ss << "]\t"; //ss.precision(3) << fixed; ss << setprecision(3) << fixed << right << distFrOrigin < to sort the p2dVect vector { bool operator() (Point2D a, Point2D b) { return a.getScalarValue() > b.getScalarValue(); } }; int main (){ list PointList; PointList.push_back( Point2D(-9, -9)); PointList.push_back( Point2D(-99, -99)); PointList.push_back( Point2D(-999, -999)); PointList.push_back( Point2D(-999, -999)); PointList.push_back( Point2D(-999, -999)); PointList.push_back( Point2D(-99, -99)); PointList.push_back( Point2D(-9, -9)); PointList.push_back( Point2D(3, 3)); PointList.push_back( Point2D(23, 23)); PointList.push_back( Point2D(123, 123)); cout << "\n X Y Dist. Fr Origin" << endl; cout << "- - - - - - - - - - - - - - - - - - -"<< endl; list::iterator p = PointList.begin(); while (p != PointList.end()){ cout << p->toString(); p++; } PointList.sort(SortByDist()); cout << "\n X Y Dist. Fr Origin" << endl; cout << "- - - - - - - - - - - - - - - - - - -"<< endl; p = PointList.begin(); while (p != PointList.end()){ cout << p->toString(); p++; } PointList.unique(); cout << "\n X Y Dist. Fr Origin" << endl; cout << "- - - - - - - - - - - - - - - - - - -"<< endl; p = PointList.begin(); while (p != PointList.end()){ cout << p->toString(); p++; } system("pause"); return 0; }