Learn C++ STL

Lesson 2 of 9 · set and multiset: Sorted, Unique, Found in log n

Module 8 · set and multiset: Sorted, Unique, Found in log n

Every set and multiset Operation, One by One, With Its Cost

Free with an accountReading

Bob keeps a set of a million numbers and often asks for the first one at or above some x. Module 2 used lower_bound(v.begin(), v.end(), x) on a sorted vector, so Bob writes the same call on his set. It compiles, and every answer is right. Yet each call took 32 to 63 milliseconds in this lesson's measurement, and his program makes thousands of them. The cost lines below explain why, and give Bob a call that takes a few microseconds at most.

Checking what this lesson needs for you…

What is inside Every set and multiset Operation, One by One, With Its Cost

  1. Making one: empty, from a list, from a range, or a copy
  2. insert, and the bool it returns
  3. insert with a hintIntermediate
  4. erase: by value, by iterator, or a range
  5. Erasing one copy from a multiset
  6. find and count
  7. lower_bound, upper_bound and equal_range
  8. Why the member lower_bound beats the free one
  9. begin, rbegin and prev(end): the two ends
  10. size, empty, clear and swap
  11. The walk: range-for, ++it and --it
  12. == and < on two sets
  13. extract and mergeIntermediate
  14. What a set does not have
  15. Every cost in one table, and the measurement