Learn C++ STL

Lesson 5 of 9 · string: Text That Knows Its Own Length

Module 3 · string: Text That Knows Its Own Length

Under the Hood: The Small String Buffer, Growth and the Cost of +

ProReading

Bob builds his contest answer with ans = ans + to_string(x) + ' ';, and the biggest test gets "Time limit exceeded". Amara writes ans += to_string(x) + ' '; and it passes in a blink. Maria asks her question: "But why, when the text is the same?" This lesson opens the string, counts what it does in memory, and measures it on a real compiler.

Checking what this lesson needs for you…

What is inside Under the Hood: The Small String Buffer, Growth and the Cost of +

  1. A string is 32 bytes: a pointer, a size and 16 more
  2. The small string buffer, counted
  3. Growth: 15, 30, 60, and why it doubles
  4. The cost of s = s + c
  5. A c_str() pointer does not survive a reallocation
  6. string against the C library
  7. Copy-on-write, and the name std::__cxx11
  8. The bytes again: two ways to write য়
  9. The performance lab