我正在开发一个 C++ 应用程序。
我有 2 个点向量
vector<Point2f> vectorAll;
vector<Point2f> vectorSpecial;
Point2f 定义typedef Point_<float> Point2f;
vectorAll 有 1000 点,而 vectorSpecial 有 10 点。
第一步:
我需要根据向量All 中的顺序对向量特殊中的点进行排序。
所以像这样:
For each Point in vectorSpecial
Get The Order Of that point in the vectorAll
Insert it in the correct order in a new vector
我可以做一个双循环并保存索引。然后根据索引对点进行排序。然而,当我们有很多点时,这种方法花费的时间太长(例如,vectorAll 中有 10000 个点,vectorSpecial 中有 1000 个点,因此需要进行一千万次迭代)
有哪些更好的方法可以做到这一点?
第二步:
vectorSpecial 中的某些点可能在 vectorAll 中不可用。我需要取最接近它的点(通过使用通常的距离公式sqrt((x1-x2)^2 + (y1-y2)^2)
)
这也可以在循环时完成,但如果有人对更好的方法有任何建议,我将不胜感激。
非常感谢您的帮助
您可以使用std::sort
on vectorAll
与Compare
功能设计考虑到的内容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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)