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 +
- A string is 32 bytes: a pointer, a size and 16 more
- The small string buffer, counted
- Growth: 15, 30, 60, and why it doubles
- The cost of s = s + c
- A c_str() pointer does not survive a reallocation
- string against the C library
- Copy-on-write, and the name std::__cxx11
- The bytes again: two ways to write য়
- The performance lab