# C++STL standard library learning notes sort

preface:

Recently, I'm learning STL standard library, making a note and sorting it out, which is convenient for me to sort out my knowledge, find it later, and facilitate others to learn. It's the best of both worlds. It's fast!

Here, I will take 12 chapters and 13 chapters of "programming and algorithm (I) C language programming" by Guo Wei of Peking University in Mu class of China University as learning materials. And I'll attach the code (as long as I'm not lazy). If there is something I haven't learned, I will continue to find materials to update my study notes.

STL (Standard Template Library)

Contains some commonly used algorithms, such as sorting and searching, and commonly used data structures, such as variable length arrays, linked lists, dictionaries, etc.

Advantages: easy to use, high efficiency.

Note: to use one of these algorithms, #include < algorithm >

1, sort

In essence, it is realized by fast sorting algorithm.

Usage 1: sort arrays of basic types (int,double,char...) from small to large

Sort (array name + n1, array name + n2);

n1 and n2 are int type expressions and can contain variables

If n1=0, + n1 may not be written

The function is to sort the elements in the array with subscript range [n1,n2) from small to large, and the elements with subscript n2 are not in the sorting interval.

Example:

1 int main(int argc, char const *argv[])
2 {
3     int a[] = {15,4,3,8,7,2,6};
4     sort(a,a+7);//Sort the entire array from small to large
5     for (int i = 0; i < 7; i++)
6     {
7         cout<<a[i]<<' ';
8     }//Results: 2 3 4 6 7 8 15
9     cout<<endl;
10     int b[] = {15,4,3,8,7,2,6};
11     sort(b,b+3);
12     for (int i = 0; i < 7; i++)
13     {
14         cout<<b[i]<<' ';
15     }//Results: 3 4 15 8 7 2 6
16     cout<<endl;
17     int c[] = {15,4,3,8,7,2,6};
18     sort(c+2,c+5);
19     for (int i = 0; i < 7; i++)
20     {
21         cout<<c[i]<<' ';
22     }//Results: 15 4 3 7 8 2 6
23     cout<<endl;
24     return 0;
25 }

Usage 2: sort the basic type array with element type T from large to small:

Sort (array name + n1, array name + N2, greater < T >);

Example:

1 int main(int argc, char const *argv[])
2 {
3     int a[] = {15,4,3,8,7,2,6};
4     int i;
5     sort(a,a+7,greater<int>());
6     for ( i = 0; i < 7; i++)
7     {
8         cout<<a[i]<<' ';
9     }//Results: 15 8 7 6 4 3 2
10
11     return 0;
12 }

Usage 3: sort arrays of any type T with custom sorting rules

The reason for using this method: when we define some classes / structures, the built-in sorting rules may not be used, or we need to use our own methods to sort. At this time, we need to customize the sorting rules.

Sort (array name + n1, array name + n2, collation structure name ());

Definition method of collation structure:

1 struct Structure name
2 {
3     bool operator()(const T & a1,const T & a2)const
4     {
5         //if a1 belong a2 Previous, return true
6         //Otherwise return false
7     }
8 };

In the sentence of boost operator () (const T & A1, const T & A2) const, remember to write const, otherwise there may be strange errors.

Example 1 (custom sorting rule):

1 struct rule1//Sort from large to small
2 {
3     bool operator()(const int & a1,const int & a2)const
4     {
5         return a1 > a2;
6     }
7 };
8 struct rule2//Sort by single digit from large to small
9 {
10     bool operator()(const int & a1,const int & a2)const
11     {
12         return a1%10 < a2%10;
13     }
14 };
15 void Print(int a[],int size)
16 {
17     for (int i = 0; i < size; i++)
18     {
19         cout<<a[i]<<",";
20     }
21     cout<<endl;
22 }
23 int main(int argc, char const *argv[])
24 {
25     int a[] = {12,45,3,98,21,7};
26     sort(a,a+sizeof(a)/sizeof(int));//Sort from small to large
27     cout<<"1)";Print(a,sizeof(a)/sizeof(int));
28     //Results: 1)3,7,12,21,45,98,
29     sort(a,a+sizeof(a)/sizeof(int),rule1());//Sort from large to small
30     cout<<"2)";Print(a,sizeof(a)/sizeof(int));
31     //Results: 2)98,45,21,12,7,3,
32     sort(a,a+sizeof(a)/sizeof(int),rule2());//Sort by single digit from small to large
33     cout<<"3)";Print(a,sizeof(a)/sizeof(int));
34     //Result: 3)21,12,3,45,7,98,
35     return 0;
36 }

Example 2 (structure array sorting):

1 struct Student
2 {
3     char name[20];
4     int id;
5     double gpa;
6 };
7 Student students [] =
8 {
9     {"Jack",112,3.4},{"Mary",102,3.8},{"Mary",117,3.9},
10     {"Ala",333,3.5},{"Zero",101,4.0}
11 };
12 //The sorting range is[n1,n2)Element of
13 //When using binary search, the search rule must be consistent with the sorting rule
14 struct StudentRule1//From small to large by name
15 {
16     bool operator()(const Student & s1,const Student & s2)const{
17         if (stricmp(s1.name,s2.name) < 0)
18         {
19             return true;
20         }
21         return false;
22     }
23 };
24 struct StudentRule2//Press id From small to large row
25 {
26     bool operator()(const Student & s1,const Student & s2)const{
27         return s1.id < s2.id;
28     }
29 };
30 struct StudentRule3//Press gpa From high to low
31 {
32     bool operator()(const Student & s1,const Student & s2)const{
33         return s1.gpa > s2.gpa;
34     }
35 };
36 void PrintStudents(Student s[],int size)
37 {
38     for (int i = 0; i < size; i++)
39     {
40         cout<<"("<<s[i].name<<","<<s[i].id<<","<<s[i].gpa<<")";
41     }
42     cout<<endl;
43 }
44 int main()
45 {
46     int n = sizeof(students)/sizeof(Student);
47
48     sort(students,students+n,StudentRule1());
49     PrintStudents(students,n);
50     //result:(Ala,333,3.5)(Jack,112,3.4)(Mary,102,3.8)(Mary,117,3.9)(Zero,101,4)
51     sort(students,students+n,StudentRule2());
52     PrintStudents(students,n);
53     //result:(Zero,101,4)(Mary,102,3.8)(Jack,112,3.4)(Mary,117,3.9)(Ala,333,3.5)
54     sort(students,students+n,StudentRule3());
55     PrintStudents(students,n);
56     //result:(Zero,101,4)(Mary,117,3.9)(Mary,102,3.8)(Ala,333,3.5)(Jack,112,3.4)
57     return 0;
58 }

These are the contents of this note. Thank you for reading here. I wish you a happy study and a permanent hair!

Posted on Mon, 06 Dec 2021 14:58:34 -0500 by psunshine