Sorting of int-type vector data is very simple, such as: vector <int> vectemp;
You can use the sort (vectemp. Begin (), vectemp. End () function. The default sorting is from small to large.
Note: The sort function requires the # include <algorithm> header file andUsing
NamespaceSTD;
For other types of Vector Data, you need to write the sorting function by yourself and call the Syntax:
Sort (vectemp. Begin (), vectemp. End (), comfun); comfun is a self-defined sorting function. Specifies what rules are "less"
Example:
Define struct
Typedef
StructN_elem
{
LongNID;
DoubleDvalue;
} Nodeelem;
//Compare predicted values
BoolCompprevalue (nodeelem first, nodeelem second)
{
If(First. dvalue <= second. dvalue) // It ranges from large to small. If you want to change from small to large, you can change it to greater than the number.
{
Return
False;
}
Else
{
Return
True;
}
}
//Sort, from large to small
Sort (vecpreresult. Begin (), vecpreresult. End (), compprevalue );
Note: The first and second parameters of the sort function specify the start position (included) and end position (not included) for sorting, that is, [a, B ), the third parameter is a custom comparison function.