Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct Point {
- int x; int y;
- int Dist2() const { return x * x + y * y;}
- // bool operator<(const Point& right) const { return x < right.x; }
- };
- Point operator-(const Point& a, const Point& b) { return {a.x - b.x, a.y - b.y}; }
- // 1. Оператор <
- bool operator<(const Point& left, const Point& right) { return left.x < right.x; }
- void BubbleSort1(vector<Point>& v) {
- int n = v.size();
- for (int i = 0; i < n - 1; ++i) {
- for (int j = 0; j < n - 1; ++j) {
- if (v[j + 1] < v[j]) swap(v[j], v[j + 1]);
- }
- }
- }
- // 2. Функция сравнения
- bool CompByDist(const Point& left, const Point& right) {
- return left.Dist2() < right.Dist2();
- }
- void BubbleSort2(vector<Point>& v, bool (* comp)(const Point&, const Point&)) {
- int n = v.size();
- for (int i = 0; i < n - 1; ++i) {
- for (int j = 0; j < n - 1; ++j) {
- if (comp(v[j + 1], v[j])) swap(v[j], v[j + 1]);
- }
- }
- }
- // 3. Функтор
- struct CompByDistF {
- Point center;
- bool operator()(const Point& left, const Point& right) const {
- return (left - center).Dist2() < (right - center).Dist2();
- }
- };
- template <class Comp>
- void BubbleSort(vector<Point>& v, Comp comp) {
- int n = v.size();
- for (int i = 0; i < n - 1; ++i) {
- for (int j = 0; j < n - 1; ++j) {
- if (comp(v[j + 1], v[j])) swap(v[j], v[j + 1]);
- }
- }
- }
- int main() {
- vector<Point> v = {{4, 2}, {3, 5}, {1, 9}, {7, 4}, {4, 3}};
- // BubbleSort(v, CompByDist);
- // CompByDistF compf;
- // compf.center = {5, 5};
- // BubbleSort(v, compf);
- Point center = {5, 5};
- // BubbleSort(v, [¢er](const Point& left, const Point& right) {
- // return (left - center).Dist2() < (right - center).Dist2();
- // });
- sort(v.begin(), v.end(), [¢er](const Point& left, const Point& right) {
- return (left - center).Dist2() < (right - center).Dist2();
- });
- for (auto& p : v)
- cout << p.x << " " << p.y << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement