forked from pz4kybsvg/Conception
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.1 KiB
45 lines
1.1 KiB
2 years ago
|
#include <iostream>
|
||
|
#include <queue>
|
||
|
#include <vector>
|
||
|
#include <cmath>
|
||
|
#include <string>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
// 位置信息类
|
||
|
class Position {
|
||
|
public:
|
||
|
Position(double x, double y, double z) : _x(x), _y(y), _z(z) { }
|
||
|
double distance() const { return sqrt(_x*_x + _y*_y + _z*_z); }
|
||
|
friend bool operator<(const Position& a, const Position& b) { return a.distance() > b.distance(); } // 定义比较函数,用于排序
|
||
|
friend ostream& operator<<(ostream& os, const Position& pos) {
|
||
|
os << "{\"x\":" << pos._x << ",\"y\":" << pos._y << ",\"z\":" << pos._z << "}";
|
||
|
return os;
|
||
|
}
|
||
|
private:
|
||
|
double _x;
|
||
|
double _y;
|
||
|
double _z;
|
||
|
};
|
||
|
|
||
|
// 队列类
|
||
|
class PositionQueue {
|
||
|
public:
|
||
|
PositionQueue() { }
|
||
|
void insert(const Position& pos) { _queue.push(pos); } // 插入
|
||
|
void output_sorted() { // 输出
|
||
|
vector<Position> positions;
|
||
|
while (!_queue.empty()) {
|
||
|
positions.push_back(_queue.top());
|
||
|
_queue.pop();
|
||
|
}
|
||
|
for (auto& pos : positions) {
|
||
|
cout << pos << endl;
|
||
|
}
|
||
|
}
|
||
|
private:
|
||
|
priority_queue<Position> _queue;
|
||
|
};
|
||
|
|
||
|
|