Learn C++ STL

Lesson 4 of 9 · The Algorithms You Will Use Weekly: accumulate to nth_element

Module 14 · The Algorithms You Will Use Weekly: accumulate to nth_element

When to Use an Algorithm, and When a Plain Loop Is Clearer

Free with an accountReading

Kenji needs the largest of a million readings, so he sorts all of them and reads the last one. Bob needs a median and writes a 12-line loop for it. His answer is wrong whenever the count is even. Both programs run. Amara asks each of them one question: what did the job need? This lesson is her answer, with a chart, a flowchart, three measurements and the cases where the loop wins.

Checking what this lesson needs for you…

What is inside When to Use an Algorithm, and When a Plain Loop Is Clearer

  1. Five questions before you write a loop
  2. What the Core Guidelines say
  3. The comparison chart
  4. The decision flowchart
  5. Measured: an algorithm against the loop you would write
  6. Measured: sort, partial_sort and nth_element for the same question
  7. When the loop is clearer
  8. The honest default
  9. When not to use them