Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- struct City
- {
- double distance;
- std::vector<int> coordinates;
- bool operator<(const City& city) const {
- return this->distance < city.distance;
- }
- City(double d) : distance(d) {}
- City(const City &city) {
- this->distance = city.distance;
- this->coordinates = city.coordinates;
- }
- City& operator= (const City &city) {
- this->distance = city.distance;
- this->coordinates = city.coordinates;
- return *this;
- }
- // same for the greater-than operator but changing "<" to ">"
- };
- struct CitySortHelper {
- bool operator() (const City &x, const City &y) const { return x < y; }
- } city_sort;
- int main() {
- vector<City> cc;
- City a(1);
- City b(2);
- City c(11);
- City d(5);
- cc.push_back(a);
- cc.push_back(b);
- cc.push_back(c);
- cc.push_back(d);
- std::sort(cc.begin(), cc.end(), city_sort);
- for (int i = 0; i < cc.size(); i++) {
- cout << cc[i].distance << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement