Advertisement
Guest User

City Sort

a guest
Dec 18th, 2011
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct City
  7. {
  8.     double distance;
  9.     std::vector<int> coordinates;
  10.     bool operator<(const City& city) const {
  11.         return this->distance < city.distance;
  12.     }
  13.     City(double d) : distance(d) {}
  14.     City(const City &city) {
  15.         this->distance = city.distance;
  16.         this->coordinates = city.coordinates;
  17.     }
  18.  
  19.     City& operator= (const City &city) {
  20.         this->distance = city.distance;
  21.         this->coordinates = city.coordinates;
  22.         return *this;
  23.     }
  24.     // same for the greater-than operator but changing "<" to ">"
  25. };
  26.  
  27. struct CitySortHelper {
  28.     bool operator() (const City &x, const City &y) const { return x < y; }
  29. } city_sort;
  30.  
  31. int main() {
  32.     vector<City> cc;
  33.     City a(1);
  34.     City b(2);
  35.     City c(11);
  36.     City d(5);
  37.     cc.push_back(a);
  38.     cc.push_back(b);
  39.     cc.push_back(c);
  40.     cc.push_back(d);
  41.     std::sort(cc.begin(), cc.end(), city_sort);
  42.     for (int i = 0; i < cc.size(); i++) {
  43.         cout << cc[i].distance << endl;
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement