Sorting structures. STL Part 13

sorting structures c ++, stl beginnersShown 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.

Newsletter of programming:

Sorting structures. STL Part 13
Rate this article

About Olej

practical experience about software development 40 years. Teacher Global Logic international software company. IBM Developer Works Permanent author of publications. Scientific editor of the computer literature publishing house "Symbol-Plus", St. Petersburg.

2 thoughts on “Sorting structures. STL Part 13

  1. А сможете аналогично растолковать функцию find(…)?
    Пытался самостоятельно написать поиск по вектору для структуры типа

    по id-элементу (при том элементов с одинаковым id быть не может).
    Пробовал писал функтор типа

    и посылать его с уже инициализированым id (создавал объект функтора с параметрами и кидал уже его) — нахватал кучку ошибок вродефункции find(★аргументы★) не существует”.
    Пробовал аналогично с лямбдой — всё тоже самое
    Подозреваю себя в географически неправильном расположении рук

Leave a Reply

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