我想通过距离变量对“mystruct”进行排序,这样做的最快方法是什么?排序矢量的最快方法是什么?
struct MyStruct {
int scale;
bool pass;
float distance;
};
vector<MyStruct> mystruct;
...
sort (mystruct.begin(), mystruct.begin() + mystruct.size());
//this doesn't work since is trying to sort by "MyStruct" and not by a number
如果我有一个
vector<float> myfloat;
...
sort (myfloat.begin(), myfloat.begin() + myfloat.size());
然后将很好地工作。
定义一个比较函数,然后使用'std :: sort'。见例如这里:http://cplusplus.com/reference/algorithm/sort/。 – 2012-07-07 14:03:09
可能的重复http://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects – jogojapan 2012-07-07 15:17:38
[按对象的属性排序对象向量](http:// stackoverflow。 com/questions/5174115 /通过对象属性排序对象) – ildjarn 2012-07-07 15:39:22