Unit 5 of 9 · about 4 hours · explain

Templates, containers and iterator invalidation

Native APIs commonly expose templates and STL types. Reading their contracts is enough to make bounded changes without learning advanced metaprogramming.

You'll be able to

  • Read a function or class template and identify its type arguments and required operations.
  • Explain container choice using contiguity, access complexity and iterator stability.
  • Determine which handles survive a vector mutation without dereferencing invalid ones.

Covers

Templates and generic code at reading depthContainer, iterator and invalidation contracts
1

Read

In this order, only the parts listed

0 of 6 done

  1. Function TemplatesAlgoMaster C++

    Anatomy of a Function Template; Template Argument Deduction; Explicit Template Arguments; Implicit vs Explicit Instantiation

    Stop at reading instantiation syntax. Distinct instantiations are not a guarantee of distinct emitted machine-code bodies.

  2. Class TemplatesAlgoMaster C++

    Declaring a Class Template; Multiple Type Parameters; Member Functions: In-Class vs Out-of-Class; Member Type Aliases; Nested Types and the typename Keyword

    Skip member templates, friend templates and metaprogramming.

  3. std::vectorAlgoMaster C++

    Memory Layout: Contiguous Storage; Size vs Capacity; Removing Elements; Accessing Elements; reserve, shrink_to_fit, resize; Iterator Invalidation; Complexity Summary

  4. IteratorsAlgoMaster C++

    The Generalised Pointer through const_iterator and Read-Only Access; Iterator Invalidation

    Skip reverse, insert and stream adapters for this pass.

  5. Container contractscppreference

    Sequence containers; Associative containers; Unordered associative containers; Iterator invalidation table

  6. Vector invalidationcppreference

    Iterator invalidation table, particularly reserve, push_back, insert and erase

2

Focus on

  • A template describes compile-time generic code. Identify the operations its body needs from T, and read the first diagnostic referring to your instantiation before the library backtrace.
  • vector<T> is contiguous for ordinary T, but vector<bool> is a special case. list provides different stability guarantees; unordered containers can invalidate iterators on rehash while preserving references to elements.
  • reserve changes capacity, not size. Existing elements remain valid after reserve only when no reallocation occurs.
  • Without reallocation, push_back still invalidates the old end iterator; erase invalidates handles at and after the erased position. Use the iterator returned by erase to continue traversal.
Common mistakes · 3
  • An iterator is not necessarily a pointer or random-access. Algorithms impose iterator-category requirements.
  • Capacity is allocated space, not a range of constructed elements that operator[] may access.
  • A sanitizer's silence does not make an invalidated iterator valid. Its address may still lie in allocated memory.
3

Try it yourself

Closed-book, before checking anything

With g++ 15.2 -std=c++20 -O0 -g, write template<class T> T twice(T x) { return x + x; }. Check twice(3) == 6 and twice(2.5) == 5.0, and explain why a type without a suitable + cannot instantiate the body. For vector<int>{1,2,3}, erase begin()+1 and check the resulting values {1,3} and that the returned iterator points to 3. Predict the invalidated handles before running. Force reallocation using reserve(capacity()+1), then reacquire all handles without comparing or dereferencing the old ones.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.A vector has spare capacity. Does push_back preserve every iterator?Answer

    No. Existing element iterators survive without reallocation, but the old past-the-end iterator is invalidated.

  2. 2.After reserve(100) on an empty vector, can you write v[0]?Answer

    No. Its size is still zero. Use resize or insert an element first.

  3. 3.Why can a template definition in a .cpp file produce a link failure for a caller in another translation unit?Answer

    The caller may need the definition to instantiate its argument types. Put the definition where it is visible or supply the needed explicit instantiation in a linked translation unit.

  4. 4.Why use it = v.erase(it) rather than ++it after erasure?Answer

    The erased iterator is invalid. erase returns a valid iterator to the following element, or the new end.

5

Practice

  • AlgoMaster Exercise: Iterators Basics

    Exercises 1 to 5, ending with Exercise 5: Refresh an Iterator After Vector Insert

    Before Exercise 5, write which handles the insert may invalidate per the cppreference table, then reacquire them.

  • AlgoMaster Exercise: Function Templates

    Exercises 1 to 4: one and two type parameters, an explicit return-type argument, and overload choice

    For each template, list the operations its body needs from each type argument.

You're done when

You explain twice's type requirements and the vector erasure result, and correctly classify handles after reserve, push_back, insert, erase and unordered-container rehash.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 8 more in the section
  • AlgoMaster C++AlgoMaster · primary

    Use the sections on compilation, pointers and references, memory, RAII and smart pointers, copy and move, STL containers, templates, errors, concurrency and paired exercises. Skip competitive-programming breadth already covered in Algorithms.

  • cppreferencecppreference community · support

    Consult language lifetime, object model, value categories, memory model, atomics, containers and iterator invalidation while reading or modifying code.

Also in this section

  • C++ Core GuidelinesStandard C++ Foundation community · required

    Use the Philosophy, Interfaces, Resource management, Expressions, Containers and concurrency rule groups to review ownership and safety decisions in the native component.

  • Clang AddressSanitizer and UndefinedBehaviorSanitizerLLVM · required

    Compile seeded memory defects with AddressSanitizer and UndefinedBehaviorSanitizer, preserve the reports, repair the root cause and keep regression tests.

  • pybind11 documentationpybind11 project · required

    Follow First steps and NumPy support to expose one small native function with explicit shape, dtype, stride, ownership, GIL and exception behavior.

  • CMake tutorialCMake · support

    Use the introductory steps for targets, libraries, testing and install rules. Produce one clean debug build and one optimized build without copied one-off commands.

  • Algorithmica - AoS and SoASergey Slotin · support

    Read the AoS and SoA experiment for a measured case where keeping each record together wins, the counterpart to a single-field scan.

  • `ld.so`: dynamic linker/loaderLinux man-pages project · support

    Read DESCRIPTION, Dynamic string tokens and LD_DEBUG to distinguish link-time symbols from runtime shared-library discovery on Linux.

  • Memory Layout TransformationsIntel · support

    Read Overview and Topics through AOS Form and SOA Form for field-wise access and locality trade-offs. Skip Intel array-notation syntax and the LIBOR example.

  • GDB manual: BacktracesGNU Project · support

    Use section 8.2 for frame numbers, bt, bt -full and optimized-out arguments when inspecting a native failure.