c++ - what is the fastest way of sorting a vector? -
i want sort "mystruct" distance variable, fastest way of doing this?
struct mystruct { int scale; bool pass; float distance; }; vector<mystruct> mystruct; ... sort (mystruct.begin(), mystruct.begin() + mystruct.size()); //this doesn't work since trying sort "mystruct" , not number
if had a
vector<float> myfloat; ... sort (myfloat.begin(), myfloat.begin() + myfloat.size());
then work perfectly.
you need write own operator<
struct.
it should like
bool operator<( const mystruct& s1, const mystruct& s2 ) { // compare them somehow , return true, if s1 less s2 // case, far understand, write // return ( s1.distance < s2.distance ); }
the other option write functional object, it's not necessary here, writing operator<
easier (for beginners)
Comments
Post a Comment