如何排序向量的点基于一个y轴
How to Sort Vector of Points Based on a Y-Axis?
我有一组坐标,例如:
10, 40;9日,27日;5, 68;7, 55岁;8、15;
如何对这些坐标排序而不丢失正确的y轴的x轴。
在上面的例子中,我想对坐标进行排序,这样正确的输出将是:
8、15;9日,27日;10、40;7, 55岁;68 .
任何建议都将非常感谢。谢谢你。
std::sort文档
#include "opencv2/core/core.hpp"
#include <algorithm> // std::sort
// This defines a binary predicate that,
// taking two values of the same type of those
// contained in the list, returns true if the first
// argument goes before the second argument
struct myclass {
bool operator() (cv::Point pt1, cv::Point pt2) { return (pt1.y < pt2.y);}
} myobject;
int main () {
// input data
std::vector<cv::Point> pts(5);
pts[0] = Point(10,40);
pts[1] = Point(9,27);
pts[2] = Point(5,68);
pts[3] = Point(7,55);
pts[4] = Point(8,15);
// sort vector using myobject as comparator
std::sort(pts.begin(), pts.end(), myobject);
}
您需要指定如何准确地存储您的坐标组。
最简单的方法是将它们存储为您创建的新结构体,并在其上应用基本的冒泡排序算法,使用Y值作为排序参数。然后当你"交换"结构体的位置时,X &你们待在一起。
struct Vector {
float x;
float y;
};
您可以创建一个映射坐标的类,如果您使用STL作为矢量,则可以使用sort方法根据Y坐标对整个矢量进行排序。
这里和这里是来自stack的类似问题
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 为什么一个向量上的多线程操作很慢
- 将一个向量插入另一个向量的某个位置
- 将指向给定子类的指针从一个向量复制到另一个向量
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 使用 std::transform 将向量向量 (a) 添加到另一个向量向量 (b)
- 如何按另一个向量的方向调整一个向量?
- 将字符向量复制到另一个向量
- 制作一对共享指针并推送一个向量
- 你能把一个向量<int64>投射到一个向量<uint8>吗
- 大家好,当一个类有一个向量作为它的数据成员时,为什么它的大小总是24?
- 为什么我们需要在优先级队列声明中添加一个向量作为参数?
- 如何将所有指针从一个向量移动到另一个向量?
- 相对于另一个向量对一个向量进行排序 - 最有效的方法
- 查找向量的一个向量的元素是否存在于另一个向量中
- 如何对一个向量相对于另一个向量进行排序
- C++ 矢量复制适用于一个向量,但不适用于另一个向量