The C++ standard template library functions sort things

  • 2020-04-02 01:44:32
  • OfStack

There's a sort function in the STL that sorts arrays directly, n times log2 of n. Sort () is defined in the header file < algorithm > In the. Sort is a function of the standard template library that sorts by knowing the starting and ending addresses, can be used to compare any container (which must satisfy a random iterator), any element, any condition, and generally performs faster than qsort. In addition, sort() is a generic function that can be used to compare any container, any element, any condition.

Specific examples are as follows:
Char ch [20] = "sdasdacsdasdas";
cout < < ch < < Endl;
Sort (ch, ch + 14);
cout < < ch < < Endl;
Note: the default is ascending sort. An example of changing sort order in sort is as follows (descending order) :

# include < iostream >
# include < algorithm >
Using namespace STD.
Bool CMP (const int a, const int b)
{
      Return a > B;
}
Int main ()
{
      The 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 or three arguments. The first parameter is the first address of the interval to be sorted, and the second parameter is the next address of the end address of the interval. That is to say, sorting interval is [a, b). Simply put, there are an array int a [100], to be from a [0] to [99] of the elements of a sort, as long as write a sort (a, a + 100), the default is ascending sort of way. Such as the need for an array of t to len - 1 0 elements of sorting, write sort (t, t + len); to sort similar vector v, sort (what exactly do v.begin (), v.e nd ()); sorting is not limited to the integer data type, as long as it is defined the less than the type of operation can be, For example, the string class string.

If there is no data type defined for the less than operation, or if you want to change the sort order, you need to use the third parameter, the comparison function. The comparison function is a self-defined function with a return value of type bool that specifies what a relationship is "less than." To arrange the array of integers in descending order, you can first define a comparison function, CMP:

Bool CMP (int a, int b)
{
      Return a > B;
Sort (a,a+100, CMP);

Suppose you define a structure node:

Struct node {
      Int a;
      Int b;
      Double c;
};
I have an array of node types, node arr[100], and I want to sort it: in ascending order of a, in descending order of b if a is the same, in descending order of c if b is still the same. You can write a comparison function like this:

Here is the code snippet:
Bool CMP (node x, node y)
{
        If (x.a! = y.a)   Return x.a
        If (x.b! = y.b)   Return x.b > Y.b;
        The return   Return x.c > Y.c.
}
Sort (arr,a+100, CMP);
Finally, look at a complete example, a title "file name sort."

Here is the code snippet:


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
//Define a structure for the File, a for the File name, and b for the File type (either "File" or "Dir")
struct node{
   string a,b;
};
//In ASCII, all uppercase letters precede all lowercase letters, 'A'<'Z' <'a' <'z'
//This one ignores case, so you can't compare strings directly. Custom an lt function, which means less than
//Convert both strings to lowercase before comparing their sizes (lexicographically)
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 function in ascending order of b (i.e. "Dir" before "File")
//If b is the same, then I'm going to rank it in ascending order of a, using the lt function that I 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;
}


Related articles: