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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With