5.2.19 · D1C++ Programming

Foundations — STL containers — vector, list, deque, array, set, multiset, map, multimap, unordered_map, unordered_set

2,637 words12 min readBack to topic

Nothing here assumes you have seen C++ before. We start at the level of "what is a box in memory" and climb, one rung at a time, to the vocabulary the parent topic uses freely.


1. A memory cell, and what "contiguous" means

Before any container, there is memory. Picture computer memory as an infinitely long street of identical houses. Each house is one byte — the smallest addressable unit — and has an address, its house number.

Figure — STL containers — vector, list, deque, array, set, multiset, map, multimap, unordered_map, unordered_set

Here is the subtlety the metaphor must not hide: one data value can occupy several bytes. A small integer might fit in 4 bytes, a large one in 8. So a value is not always one house — it may be a block of neighbouring houses. We call the number of bytes one element occupies its size, written .

WHY the topic needs this: the parent note says a vector gives " random access because index math gives the address." That is meaningless until you can see why. With the three symbols above, element begins at

Read it in words: start at the base , then skip past whole elements, each bytes wide. That is one multiply () and one add — a fixed amount of work no matter how big is. That is what "jump straight to any element" means, and it only works because the elements are in a row.


2. What "" actually says — Big-O from zero

The parent note is drenched in , , . These are not numbers. They answer one question: "as the amount of data grows, how does the work grow?"

Figure — STL containers — vector, list, deque, array, set, multiset, map, multimap, unordered_map, unordered_set

Read the figure as three growth shapes, worst (slowest) at top:

  • — flat. Work does not grow at all when grows. "Jump to element " from Section 1 is : one multiply-add whether is 10 or 10 million.
  • — barely rising. Doubling adds only a constant amount of work. This is what tree containers (set, map) give — see Section 6.
  • — straight line. Double the data, double the work. "Walk through every element looking for a value" is .
Recall Why is

an operation? Because it is always the same work — one multiply, one add (each a primitive costing ) — regardless of how many elements exist. The count never appears in the formula. ::: A fixed number of primitive operations independent of is the definition of .


3. "Amortised" — the average over a run

The parent claims push_back is amortised . That word "amortised" is doing heavy lifting.

WHY the topic needs this: without "amortised", you would look at the occasional copy during a resize and wrongly conclude push_back is slow. The full proof lives in Amortised analysis; the parent note derives the total there.


4. Pointers and nodes — the linked world

Section 1 gave us the contiguous world. The other half of the STL — list, and the internals of tree containers — lives in the linked world, and its atom is the pointer.

Figure — STL containers — vector, list, deque, array, set, multiset, map, multimap, unordered_map, unordered_set

Look at the figure. The nodes are scattered across memory — node A at 1000, node B at 5000, node C at 2000 — but the red next arrows stitch them into an order. This is the key contrast with Section 1:

  • Contiguous array: neighbours are neighbours in memory ⇒ jump to index instantly, but inserting in the middle means physically shifting every later house over.
  • Linked nodes: neighbours are wherever they happen to be; you find them only by following pointers ⇒ no jump-to-index, but inserting is just rewriting two arrows ().
Recall To insert a node between B and C in the figure, what changes?

Only four arrows: the new node's prev/next, plus B's next and C's prev. No other node moves. ::: That is why linked-list insertion (given the position) is — pure pointer surgery, no shifting.


5. Keys, values, and "sorted order"

The parent splits containers into sequence (ordered by insertion) and associative (organised by value). To read that, you need three words.


6. Trees and hash tables — the two ways to organise by key

Two engines power the associative containers. You need a mental picture of each.

Figure — STL containers — vector, list, deque, array, set, multiset, map, multimap, unordered_map, unordered_set

Trace the red path in the figure searching for 40: start at 50 (40 < 50 → go left), reach 30 (40 > 30 → go right), reach 40 ✓. Three comparisons out of seven nodes. Each level halves what is left — the same halving that makes binary search .


7. Iterators — the universal "position marker"

Finally, the word every worked example uses: iterator.

The parent's list example — L.insert(it, x) is — only works because it already holds the position. Getting the iterator to that spot may itself cost . That distinction (holding a position vs. finding it) explains half the "gotchas" in the cheat-sheet. Deep dive: Iterators in C++, and the functions that consume them: STL algorithms.


How the foundations feed the topic

Address and contiguous memory

Index math B plus i times s

Pointers and nodes

Big-O growth shapes

Amortised cost

Binary search tree

Hash table and buckets

Key value and sorted order

Iterators as position markers

STL containers topic


Equipment checklist

What is a byte, and how does it relate to an element?
A byte is the smallest addressable cell of memory (one house); an element may occupy several neighbouring bytes — of them.
Give the starting address of element , defining every symbol
, where is the base address (byte of element 0) and is the element size in bytes.
What standing assumption lets us call an step?
The unit-cost assumption: each primitive (add, multiply, dereference, key comparison, hash) costs .
Is a std::deque one contiguous block?
No — it is a segmented array of fixed-size chunks; random access is still but the memory is not one unbroken block.
What does mean in one sentence?
Work grows in a straight line with the number of elements — double the data, double the work.
What does "amortised " allow that plain forbids?
Rare expensive operations, as long as the average over a long run of operations is constant.
What is a pointer, and what does dereferencing it cost?
A value holding the address of another value; following (dereferencing) it is a primitive costing .
Why can a linked list insert in but a vector cannot?
The list only rewrites a few pointer arrows; the vector must physically shift every later element over.
In a binary search tree, where do smaller keys live relative to a node?
To its left; larger keys to its right — which lets search halve the candidates each step.
Why does a hash table give no sorted order?
The hash function deliberately scatters keys across buckets, which destroys any ordering.
What is an iterator?
A movable position marker (a "finger") pointing at one element, that can step to the next.