Sorting a vector with structs
-
Hello, I would like to sort a vector(vlines), this is a vector with struct's(line). I want to sort the vector on name and on length.
struct line{ CString name; double length; }; vector vlines;
I know there is a function sort and that it uses a predikaat function.std::sort(vlines.begin(), vlines.end(), length()); std::sort(vlines.begin(), vlines.end(), name());
But how can i code the predikaat function's? or template's??:confused: I hope someone can help me? Thanks -
Hello, I would like to sort a vector(vlines), this is a vector with struct's(line). I want to sort the vector on name and on length.
struct line{ CString name; double length; }; vector vlines;
I know there is a function sort and that it uses a predikaat function.std::sort(vlines.begin(), vlines.end(), length()); std::sort(vlines.begin(), vlines.end(), name());
But how can i code the predikaat function's? or template's??:confused: I hope someone can help me? ThanksThose predicates are anything (functions, objects) that can be called with the same syntax as a function. So, even a function can do; for instance:
inline bool compare_line_by_length(const line&x,const line& y)
{
return x.name<y.name;
}can be fed to
std::sort
like this:std::sort(vlines.begin(), vlines.end(), compare_line_by_length);
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo