根据另一个向量排序点向量

You*_*sef 9 c++ sorting vector stl-algorithm c++11

我正在开发一个C++应用程序.

我有2个点向量

vector<Point2f> vectorAll;
vector<Point2f> vectorSpecial;  
Run Code Online (Sandbox Code Playgroud)

Point2f已定义 typedef Point_<float> Point2f;

vectorAll有1000点而vectorSpecial有10点.

第一步:

我需要根据vectorAll中的顺序对vectorSpecial中的点进行排序.所以像这样:

For each Point in vectorSpecial
    Get The Order Of that point in the vectorAll
    Insert it in the correct order in a new vector
Run Code Online (Sandbox Code Playgroud)

我可以做一个双循环并保存索引.然后根据索引对点进行排序.然而,当我们有很多点时,这种方法花费的时间太长(例如,vectorAll中的10000个点和vectorSpecial中的1000个点,因此千万次迭代)

有什么更好的方法呢?

第二步:

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

这也可以在循环时完成,但如果有人对更好的方法有任何建议,我将不胜感激.

非常感谢您的帮助

Luc*_*ore 2

您可以将std::sortonvectorAllCompare旨在考虑 的内容的函数一起使用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);
Run Code Online (Sandbox Code Playgroud)