【发布时间】:2012-07-05 16:34:08
【问题描述】:
我正在开发一个 C++ 应用程序。
我有 2 个点向量
vector<Point2f> vectorAll;
vector<Point2f> vectorSpecial;
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
我可以做一个双循环并保存索引。然后根据它们的索引对点进行排序。但是,当我们有很多点时(例如,vectorAll 中的 10000 个点和 vectorSpecial 中的 1000 个点,所以这是一千万次迭代),这种方法花费的时间太长了
有什么更好的方法?
第二步:
vectorSpecial 中的某些点可能在 vectorAll 中不可用。我需要取离它最近的点(使用通常的距离公式sqrt((x1-x2)^2 + (y1-y2)^2))
循环时也可以这样做,但如果有人对更好的方法有任何建议,我将不胜感激。
非常感谢您的帮助
【问题讨论】:
-
请注意,调用 STL 算法并不能消除循环,它只是将它们隐藏在抽象层后面。
标签: c++ sorting c++11 vector stl-algorithm