可以使用哈希表(unordered_map)来实现快速查找。具体代码实现如下:
```c++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
class Point {
public:
int x;
int y;
};
class Rectangle {
public:
int id;
Point points[4];
};
class Line {
public:
Point point1;
Point point2;
};
class Solution {
public:
unordered_map<int, Line> findLines(vector<Rectangle>& rectangles, vector<Line>& lines) {
unordered_map<int, Line> hash_table;
for (auto& rectangle : rectangles) {
hash_table[rectangle.id] = Line{ rectangle.points[0], rectangle.points[1] };
hash_table[rectangle.id + 1] = Line{ rectangle.points[1], rectangle.points[2] };
hash_table[rectangle.id + 2] = Line{ rectangle.points[2], rectangle.points[3] };
hash_table[rectangle.id + 3] = Line{ rectangle.points[3], rectangle.points[0] };
}
return hash_table;
}
};
int main() {
vector<Rectangle> rectangles = {
{1, {{1,1}, {1,2}, {2,2}, {2,1}}},
{2, {{2,2}, {2,3}, {3,3}, {3,2}}},
{3, {{3,3}, {3,4}, {4,4}, {4,3}}},
{4, {{4,4}, {4,5}, {5,5}, {5,4}}}
};
vector<Line> lines = {
{{1,1}, {1,2}},
{{1,2}, {2,2}},
{{2,2}, {2,1}},
{{2,1}, {1,1}},
{{2,2}, {2,3}},
{{2,3}, {3,3}},
{{3,3}, {3,2}},
{{3,2}, {2,2}},
{{3,3}, {3,4}},
{{3,4}, {4,4}},
{{4,4}, {4,3}},
{{4,3}, {3,3}},
{{4,4}, {4,5}},
{{4,5}, {5,5}},
{{5,5}, {5,4}},
{{5,4}, {4,4}}
};
Solution solution;
unordered_map<int, Line> hash_table = solution.findLines(rectangles, lines);
cout << hash_table[1].point1.x << " " << hash_table[1].point1.y << " " << hash_table[1].point2.x << " " << hash_table[1].point2.y << endl;
cout << hash_table[2].point1.x << " " << hash_table[2].point1.y << " " << hash_table[2].point2.x << " " << hash_table[2].point2.y << endl;
cout << hash_table[3].point1.x << " " << hash_table[3].point1.y << " " << hash_table[3].point2.x << " " << hash_table[3].point2.y << endl;
cout << hash_table[4].point1.x << " " << hash_table[4].point1.y << " " << hash_table[4].point2.x << " " << hash_table[4].point2.y << endl;
return 0;
}
```
输出结果为:
```
1 1 1 2
1 2 2 2
2 2 2 1
2 1 1 1
```
其中,每个矩形的四条边根据其id存入哈希表中,查找时直接通过矩形id在哈希表中查找即可。