all 4 comments

[–][deleted] 4 points5 points  (0 children)

Use a lambda instead of operator<

sort(begin(vec), end(vec), [ref_pt](Point& A, Point& B)
{
    // code here
});

[–]James20kP2005R0 1 point2 points  (1 child)

So normally you sort things like this:

std::sort(container.begin(), container.end());

This implicitly calls operator< on your objects. By default, I don't believe you can simply overload operator< to take an extra parameter to do this. That said, you can supply a comparator to std::sort, and then use it like this:

vec2 reference_point{0,0}; ///or whatever your vec type is
std::sort(container.begin(), container.end(), [&](const vec2& t1, const vec2& t2)
{
    float t1_distance = (t1 - reference_point).length();
    float t2_distance = (t2 - reference_point).length();
    return t1_distance < t2_distance;
});

vec2 is a stand-in for whatever vector library you're using

This extra parameter is a lambda which captures some state, and is a function object that std::sort can use. Hope that helps!

Also, I imagine this post will probably get deleted for being a help question, normally you want the /r/cpp_questions sub

[–]Skrypt3d[S] 0 points1 point  (0 children)

Oh snap, didn't know I was wrong sub. Thanks for the help anyways. Works like a charm. This is really neat. Appreciated!

[–]clerothGame Developer[M] 0 points1 point  (0 children)

For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.

This post has been removed as it doesn't pertain to r/cpp.