基于另一向量来排序点的向量 [英] order a vector of points based on another vector

查看:131
本文介绍了基于另一向量来排序点的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个C ++应用程序。



我有两个点的向量

  vector< Point2f> vectorAll; 
vector< Point2f> vector特殊;

Point2f定义 typedef Point_< float> Point2f;



vectorAll有1000点,而vectorSpecial有10点。



strong>第一步:



我需要在vectorSpecial中排序点,具体取决于它们在vectorAll中的顺序。
这样的东西:

 对于vectorSpecial中的每个点
获得vectorAll
在新向量中以正确的顺序插入

并保存索引。然后基于它们的索引对点进行排序。但是,当我们有很多点时(例如,vectorAll中有10000点,vectorSpecial中有1000点,因此是一千万次迭代),这种方法需要的时间太长。





vectorSpecial中的一些点可能不是在vectorAll可用。我需要采取最接近它的点(通过使用通常的距离公式 sqrt((x1-x2)^ 2 +(y1-y2)^ 2)



这也可以循环,但如果有人有任何建议更好的方法,我会很感激。



非常感谢任何帮助

解决方案

您可以使用 std :: sort 比较函数设计的 vectorSpecial

  struct myCompareStruct 
{
std :: vector< Point2f> ;所有;
std :: vector< Point2f>特别;
myCompareStruct(const std :: vector< Point2f>& a,const std :: vector< Point2f>& s)
:all(a),special(s)
{
}
bool operator()(const Point2f& i,const Point2f& j)
{
//无论逻辑是什么
}
}

std :: vector< Point2f>所有;
std :: vector< Point2f>特别;
//填充向量
myCompareStruct compareObject(all,special);

std :: sort(special.begin(),special.end(),compareObject);


I am working on a C++ application.

I have 2 vectors of points

vector<Point2f> vectorAll;
vector<Point2f> vectorSpecial;  

Point2f is defined typedef Point_<float> Point2f;

vectorAll has 1000 point while vectorSpecial has 10 points.

First Step:

I need to order the points in vectorSpecial depending on their order in vectorAll. So something like this:

For each Point in vectorSpecial
    Get The Order Of that point in the vectorAll
    Insert it in the correct order in a new vector

I can do a double loop and save the indexes. and then order the points based on their indexes. However this method is taking too long when we have lots of points (for example 10000 points in vectorAll and 1000 points in vectorSpecial so that's ten million iteration)

What are better methods of doing that?

Second Step:

Some points in vectorSpecial might not be available in vectorAll. I need to take the point that is closest to it (by using the usual distance formula sqrt((x1-x2)^2 + (y1-y2)^2))

This also can be done when looping, but if someone has any suggestions for better methods, I would appreciate it.

Thanks a lot for any help

解决方案

You can use std::sort on vectorAll with the Compare function designed to take into account the contents of vectorSpecial:

struct myCompareStruct
{
    std::vector<Point2f> all;
    std::vector<Point2f> special;
    myCompareStruct(const std::vector<Point2f>& a, const std::vector<Point2f>& s)
        : all(a), special(s) 
    {
    }
    bool operator() (const Point2f& i, const Point2f& j) 
    { 
        //whatever the logic is
    }
};

std::vector<Point2f> all;
std::vector<Point2f> special;
//fill your vectors
myCompareStruct compareObject(all,special);

std::sort(special.begin(),special.end(),compareObject);

这篇关于基于另一向量来排序点的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆