面试题答案
一键面试#include <iostream>
#include <algorithm>
using namespace std;
struct Point {
int x;
int y;
};
// 定义比较函数
bool compare(const Point& a, const Point& b) {
if (a.x != b.x) {
return a.x < b.x;
} else {
return a.y < b.y;
}
}
int main() {
Point points[] = {{3, 4}, {1, 2}, {3, 1}};
int n = sizeof(points) / sizeof(points[0]);
sort(points, points + n, compare);
for (int i = 0; i < n; ++i) {
cout << "(" << points[i].x << ", " << points[i].y << ") ";
}
cout << endl;
return 0;
}