c++ - Sort vector based on value outside of class -
i have std::vector<mystruct>
, simple version of struct this:
struct mystruct { glm::vec3 position; };
the above struct declared in class:
class myclass { private: struct mystruct { glm::vec3 position; }; std::vector<mystruct> structvector; glm::vec3 outsideposition; };
but issue i'm having how sort structvector
based on distance between structvector.position
, outsideposition
.
i've tried using std::sort
this:
std::sort(this->structvector.begin(), this->structvector.end(), this->sortfunction);
where sortfunction
declared as:
glboolean myclass::sortfunction(mystruct const &x, mystruct const& y) { glfloat dist1 = glm::length(this->outsideposition - x.position); glfloat dist2 = glm::length(this->outsideposition - y.position); return dist1 < dist2; }
but compilation error stating there's no overloaded method std::sort
. going wrong here?
errors:
error c2672 'std::sort': no matching overloaded function found
error c3867 'myclass::sortfunction': non-standard syntax; use '&' create pointer member
std::sort
takes predicate compare element.
method requires information, have bind in way;
lambda (via capture) simple one:
std::sort(structvector.begin(), structvector.end(), [this](mystruct const &lhs, mystruct const& rhs) { return this->sortfunction(lhs, rhs); });
Comments
Post a Comment