The
STL has a sort function that can be sorted directly into the array, with a complexity of N*LOG2 (n). Sort () is defined in header file <algorithm>. The sort function is a function of the standard Template Library, where the start and end addresses are sorted, and can be used to compare any container (which must satisfy a random iterator), any element, any condition, and the execution speed is generally faster than the qsort. In addition, sort () is a generic function that can be used to compare any container, any element, any condition. The specific example of
is as follows:
char ch[20]= "Sdasdacsdasdas";
cout<<ch<<endl;
Sort (ch,ch+14);
cout<<ch<<endl;
Note: The default is ascending sorting. An example of a change in sort order in sort is as follows (descending):
#include <iostream>
#include <algorithm>
using namespace Std;
BOOL CMP (const int A, const int b)
{
return a > B;
}
int main ()
{
int data[5];
for (int i = 0; i < 5; i++)
CIN >> Data[i];
Sort (data, data + 5, CMP);
return 0;
}
This function can pass two parameters or three parameters. The first parameter is the first address of the interval to sort, and the second is the next address of the interval end address. That is, the interval of the sort is [a,b]. In simple terms, there is an array int a[100] to sort the elements from a[0 to a[99], as long as the sort (a,a+100) is written, and the default sort is ascending. If you want the No. 0 to len-1 element of the array T to be sorted, write sort (t,t+len), sort the vector v the same, sort (V.begin (), V.end ()), and the sorted data type is not limited to integers, as long as you define a type that is less than the operation, such as String.
If you do not define a data type that is less than the operation, or if you want to change the order of the sort, you need to use the third parameter-the comparison function. The comparison function is a function of its own definition, and the return value is bool, which defines what kind of relationship is "less than". To sort the array of integers in descending order, you can first define a comparison function CMP:
BOOL CMP (int a,int b)
{
Return a>b;
When sorting, write sort (a,a+100,cmp);
Suppose you define a struct node:
struct node{
int A;
int b;
Double C;
};
There is a node-type array node arr[100] that you want to sort by: first in ascending order of a value, if the value of a is the same, and then in descending order B, if B is the same, in descending order C. You can write a comparison function like this:
The following is a code fragment:
BOOL CMP (node X,node y)
{
if (X.A!=Y.A) return x.a
if (x.b!=y.b) return x.b>y.b;
return return x.c>y.c;
}
Sort by writing sort (arr,a+100,cmp);
Finally look at a complete example, a topic "file name sort".
The following is a code fragment:
Copy Code code as follows:
#include <iostream>
#include <algorithm>
#include <string>
using namespace Std;
Define a struct to represent the file, a for the filename, b for the file type (either "File" or "Dir")
struct node{
String a,b;
};
In ASCII code, all uppercase letters precede all lowercase letters, ' a ' < ' Z ' < ' a ' < ' Z '
This problem requires that you ignore the case, so you can't use string comparisons directly. Custom A LT function, which is the meaning of less than
First convert all two strings to lowercase, then size (dictionary order)
BOOL Lt (string x,string y)
{
int i;
For (I=0;i<x.length (); i++)
if (x[i]>= ' A ' &&x[i]<= ' Z ')
X[i]= ' a ' + (x[i]-' a ');
For (I=0;i<y.length (); i++)
if (y[i]>= ' A ' &&y[i]<= ' Z ')
Y[i]= ' a ' + (y[i]-' a ');
Return x<y;
}
Custom comparison functions, first in ascending order of B value (that is, "Dir" in front of "File")
If the B value is the same, then press A in ascending order, with the LT function just defined
BOOL Comp (node X,node y)
{
if (x.b!=y.b) return x.b<y.b;
Return LT (x.a,y.a);
}
int main ()
{
Node arr[10001];
int size=0;
while (CIN>>ARR[SIZE].A>>ARR[SIZE].B)
size++;
Sort (arr,arr+size,comp);
for (int i=0;i<size;i++)
cout<<arr[i].a<< "" <<arr[i].b<<endl;
return 0;
}