The basics of programming in c++ for beginners

Sorting structures. STL Part 13

Shown in the previous section various sorting - Flexible and beautiful mechanism for all occasions. That's just in practice, sort sequence almost never have a pure. This is all from the field of educational and demonstration tasks.

In practice, the more often the problem is sorted quite voluminous data structures (even bulky in size, and according to the number of its fields). And sort (or resorting) they will have on the values ​​for the different fields of these same structures. But here come to the aid STL algorithms, especially when used in conjunction with the functors.

Let's see the typical model problem, which we've seen before - a description of the training group or faculty:

The program requests the field number data, on which sorting will be carried out (actually - comparison). If this number is entered, as a positive number, then sort by that field goes in ascending order. If the number is entered with a minus sign - the sort order is reversed:

sorting structures c ++, stl beginnerssorting structures c ++, stl beginners

Note, that the sorting algorithm does not care that sort: if the numeric data, the largest, and if lowercase, in order leksograficheskom.

3 thoughts on “Sorting structures. STL Part 13

  1. And you can similarly interpret the find function(…)?
    Tried to write myself a vector search for a structure like

    by id element (moreover, there cannot be elements with the same id).
    Tried writing a functor like

    and send it with an already initialized id (created a functor object with parameters and threw it already) - picked up a bunch of errors like “find functions(★аргументы★) does not exist”.
    Tried it the same way with lambda - everything is the same…
    I suspect myself of geographically inappropriate hand placement

Leave a Reply to Olej Cancel reply

Your email address will not be published. Required fields are marked *