An STL ApplicationAssignmentThe Standard Template Library ApplicationsLecture 39Section 10.5Robb T. KoetherHampden-Sydney CollegeMon, Apr 26, 2010Robb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 1 / 9Outline1An STL Application2AssignmentRobb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 2 / 9Outline1An STL Application2AssignmentRobb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 3 / 9Convex HullThe convex hull program is somewhat simpler if we use the STLvector class.That is because of the additional member functions of the vectorclass.A ConvexHull object consists of a vector of points:vector<Point2D<int> > list;Robb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 4 / 9The ConvexHull ConstructorExample (The ConvexHull Constructor)ConvexHull(const vector<Point2D<int> >& pts){if (pts.size() < 3)list.insert(list.begin(), pts.begin(),pts.end());else{list.insert(list.begin(), pts.begin(),pts.begin() + 3);if (!hasPosOrient())reverse(list.begin(), list.end());for (int i = 3; i < pts.size(); i++)insert(pts[i]);}return;}Robb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 5 / 9The insert FunctionExample (The insert Function)void insert(const Point2D<int>& pt){if (!isInsideHull(pt)){int i = 0;while (!isLeftOfLine(i, pt))i = nextVertex(i);while (isLeftOfLine(i, pt))i = nextVertex(i);int k = i;while (!isLeftOfLine(i, pt))i = nextVertex(i);int m = i;...Robb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 6 / 9The insert FunctionExample (The insert Function)...if (m > k){list.erase(list.begin() + k + 1,list.begin() + m);list.insert(list.begin() + k + 1, pt);}else{list.erase(list.begin() + k + 1, list.end());list.erase(list.begin(), list.begin() + m);list.insert(list.begin(), pt);}}return;}Robb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 7 / 9Outline1An STL Application2AssignmentRobb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 8 / 9AssignmentHomewor kRead Section 10.5, pages 564 - 573.Robb T. Koether (Hampden-Sydney College) The Standard Template Library Applications Mon, Apr 26, 2010 9 /
View Full Document