Dry goods: explain the CMP parameters of C + + sort function in detail!

Time:2021-10-22

On the first day of learning algorithms, you were learning bubble and bucket row

When you haven’t figured out fast platoon and merge

You have learned the final heap sorting and Hill sorting of data structures

It can be said that ranking is a nightmare for many competition students


So it was born

void std::sort()

Sort the elements of a sequence using a predicate for comparison.

Parameters:
__first – An iterator.
__last – Another iterator.
__comp – A comparison functor.

The sorting is completed for an address interval, and the algorithm automatically selects each time, focusing on fast sorting

C + + requires header files#include(of course, I have no problem with universal head)

The simplest is to use it to sort int types in ascending order

#include 
#include 
using namespace std;
int main() {
    int a[5] = {2, 1, 3, 5, 4};
    sort(a, a + 5);
    for (int i = 0; i < 5; i++) cout << a[i] << " ";
}

The output is as follows, which is very simple

1 2 3 4 5

Here are the parameters passed to sortaanda + 5They are all addresses. Like most programming languages, the principle of “close left and open right” is followed here, that is, the five addresses that the function will actually read and operate are as follows:
a + 0
a + 1
a + 2
a + 3
a + 4

If descending sorting is required, the procedure is as follows

#include 
#include 
using namespace std;

bool cmp(int x, int y){
    return x > y;
}

int main() {
    int a[5] = {2, 1, 3, 5, 4};
    sort(a, a + 5, cmp);
    for (int i = 0; i < 5; i++) cout << a[i] << " ";
}

output

5 4 3 2 1

We wrote an additional CMP function of bool type and passed its address to sort as the third parameter

CMP can replace its built-in functions to determine which elements should be first and which elements should be last

Many small partners may have a doubt: how to understand CMP functions in essence, or how should I remember how to write CMP?

Let’s look at these three points:

  1. Undoubtedly, the CMP function returns bool type, indicating whether the current sorting is correct (see 3 for details)
  2. The CMP function should accept two parameters of the same type as the array to be sorted (it can be common types such as int, short and long, and of course it can also be a structure)
  3. The actual meaning of CMP return value is to pass in two parameters a and B. whether the sorting of a before B is correct. If it is correct, it returns 1 (true), otherwise it returns 0 (false)

Then let’s look at an example of sorting the structure: input the names and grades of 10 students, sort them from high to low, and then output them

input data:

Yixiangzhilv 90
Mydr 60
Xiaoming 10
Mr.Glass 60
GZN 80
Wangzi 85
Hyx 100
Wyx 99
Xth 0
Zz 75

The program is implemented as follows

#include 
#include 
using namespace std;

struct node {
    string name;
    int score;
};

bool cmp(struct node x, struct node y) {
    return x.score > y.score;
}

int main() {
    struct node a[10];
    for (int i = 0; i < 10; i++) cin >> a[i].name >> a[i].score;
    sort(a, a + 10, cmp);
    for (int i = 0; i < 10; i++) cout << a[i].name << " " << a[i].score << endl;
}

(there is also a C + + knowledge here: if the structure node has been defined, thenstruct node a[10];andnode a[10];(all legal)


Related questions:P1786 Banggong ranking – New Ecology of computer science education in Luogu

Solution:Algorithmic question brushing [Luogu p1786] help tribute sorting – algorithmic question brushing thinking carefully is extremely afraid of the phenomenon_ Fantasy blog – CSDN blog