Learn C++ STL

Lesson 3 of 9 · sort, Comparators and Lambdas: Tell the Algorithm What You Mean

Module 12 · sort, Comparators and Lambdas: Tell the Algorithm What You Mean

Full Programs: From a Race Table to a priority_queue That Takes Your Rule

Free with an accountReading

A working programmer rarely sorts plain numbers. She sorts words by length, riders by points, runners by time and books by author. Each job is the same sort call with a different rule, the comparator that answers "must a come before b?". Six programs follow, each a little bigger than the last. Read the "new thing" line above each program first; it is what that program is for.

Checking what this lesson needs for you…

What is inside Full Programs: From a Race Table to a priority_queue That Takes Your Rule

  1. Program 1: words by length, then from a to z
  2. Program 2: a race table with std::tie, one key high to low
  3. Program 3: sort the indices, not the values
  4. Program 4: sort, then walk the equal runs
  5. Program 5: a priority_queue that takes your lambdaIntermediate
  6. Program 6: records from input, a library catalogueIntermediate