对由两个点组成的线段进行排序
Ordering line segments consisting of two points
我将如何为由起点和终点组成的线段实现运算符<。我想将线段插入到地图中,这样顺序就不需要是语义的,但它应该适用于所有情况。
按字典顺序排列所有内容:
struct Point { int x; int y; };
bool operator<(Point const & a, Point const & b)
{
return (a.x < b.x) || (!(b.x < a.x) && (a.y < b.y));
}
或者使用现成的比较器tuple
:
#include <tuple>
// ...
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
或者实际上使用std::tuple<int, int>
来表示您的积分,什么都不做!
然后,对行执行相同的操作:
struct LineSegment { Point x; Point y; };
// repeat same code as above, e.g.
bool operator<(LineSegment const & a, LineSegment const & b)
{
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
}
重复一遍,完全不工作的解决方案只是一直使用元组:
typedef std::tuple<int, int> Point;
typedef std::tuple<Point, Point> LineSegment;
// everything "just works"
相关文章:
- 我不明白这段代码是如何对这个pythonlist()进行排序的,也不明白如何用C++中的向量来重现它
- Protobuf中重复字段的问题.使用重复字段进行序列化/反序列化的更好方法是什么?
- 对许多字段的元素进行排序
- 列表STL的时间复杂性在一段时间内进行排序
- 如何对段C 进行分类
- 在完成对 k 个排序流进行排序的函数时需要帮助
- 委托构造函数在使用类字段进行参数时会出现分段错误
- 如何并行化矩阵排序以进行循环
- 为什么对排序向量进行的二进制搜索比STD :: SET FIND慢
- 为什么这两个版本的快速排序在进行比较的次数上存在巨大差异
- 选择排序.如何进行选择作为稳定算法
- 为什么使用STL排序()进行二次时间排序
- 按一个字段对自定义对象向量进行排序
- 根据特定字段对结构的矢量进行排序
- 快速排序不对 C++ 进行排序
- 一个函数,它将显示使用插入排序对 c++ 进行排序的数组的内容
- 使用静态方法/字段 c++ 进行继承
- C# 与 C/C++:我是否需要手动对结构字段进行排序以获得最佳性能
- 对两个不同字段进行排序并确定其唯一性的容器
- 如何用O(n)个时间和O(1)个空间对n个对象的群进行排序.每个对象有两个字段:int和string