Examples of C ++ sort () function usage and sort instance usage

Source: Internet
Author: User

Examples of C ++ sort () function usage and sort instance usage

C ++ sort () function usage

I recently read the c ++ standard library and learned a lot. I will write down the usage of the C ++ sort () function and share it with you!

(1) Why should I use the sorting function in the c ++ standard library?

The Sort () function is one of the c ++ sorting methods, I learned this method to eliminate the low execution efficiency caused by Bubble sorting and sorting! Because the sorting method is similar to the fast sorting method, the time complexity is n * log2 (n), and the execution efficiency is high!

(2) How to Use the sorting functions in the c ++ standard library

I) The Sort function is included in the c ++ standard library with the header file # include. Calling the sorting method in the standard library does not have to know how it is implemented internally, you only need to display the desired result!

II) The Sort function has three parameters:

(1) The first is the starting address of the array to be sorted.

(2) The second is the ending address (the last address to be sorted)

(3) The third parameter is the sorting method. It can be from large to small, but from small to large. The third parameter can also be left blank. The default sorting method is from small to large.

Template used by the Sort function:

Sort (start, end, sorting method)

The following describes how to use the sort () function to sort dozens of numbers in a logarithm group!

Example 1: The sort function does not have the third parameter, but is implemented from small to large.

#include<iostream>  #include<algorithm>  using namespace std;  int main()  {   int a[10]={9,6,3,8,5,2,7,4,1,0};   for(int i=0;i<10;i++)   cout<<a[i]<<endl;  sort(a,a+10);   for(int i=0;i<10;i++)   cout<<a[i]<<endl;   return 0;  }  

Example 2

Through the above example, you will have a question: do you want to achieve a swollen sorting from big to small?

This is as mentioned above. We need to make an article in The third parameter of the sort () function, telling the program that I want to sort from big to small!

You need to add a comparison function complare (). The implementation process of this function is as follows:

Bool complare (int a, int B)

{

Return a> B;

}

This is the way to tell the program to implement sorting from big to small!

# Include <iostream> # include <algorithm> using namespace std; bool complare (int a, int B) {return a> B;} int main () {int a [10] = {9, 6,}; for (int I = 0; I <10; I ++) cout <a [I] <endl; sort (a, a + 10, complare); // you do not need to input parameters to the complare function here, // This is the rule for (int I = 0; I <10; I ++) cout <a [I] <endl; return 0 ;}

Example 3:

Although the method in the first and second examples above achieves sorting from big to small and from big to small, it is still a little troublesome to do so, because you also need to write a function that tells the program what sort principle to execute, the powerful functions of the c ++ standard library can completely solve this problem.

The third parameter of the Sortt function can be used to tell the program the sorting principle you adopt.

Less <data type> () // sort data in ascending order

Greater <data type> () // sort data in ascending order

In combination with this example, you can complete any sort principle you want.

#include<iostream>  #include<algorithm>  using namespace std;  int main()  {   int a[10]={9,6,3,8,5,2,7,4,1,0};   for(int i=0;i<10;i++)   cout<<a[i]<<endl;  sort(a,a+10,less<int>());   for(int i=0;i<10;i++)   cout<<a[i]<<endl;   return 0;  }  #include<iostream>  #include<algorithm>  using namespace std;  int main()  {   int a[10]={9,6,3,8,5,2,7,4,1,0};   for(int i=0;i<10;i++)   cout<<a[i]<<endl;   sort(a,a+10,greater<int>());   for(int i=0;i<10;i++)   cout<<a[i]<<endl;   return 0;  }  

Example 4: Use the sort function to sort the characters. The sorting method is similar. The following shows an example of the program.

#include<iostream>  #include<algorithm>  using namespace std;  int main()  {   char a[11]="asdfghjklk";   for(int i=0;i<10;i++)   cout<<a[i]<<endl;   sort(a,a+10,greater<char>());   for(int i=0;i<10;i++)   cout<<a[i]<<endl;   return 0;  }  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.