skip to content

STL Containers

vector, map, unordered_map and their siblings each have a cost model and invalidation rules. Interviewers expect you to pick vector by default and say why.

on this pageshow

explore

questions

page 2 of 2

In C++20, why may a range's end() return a different type from its begin(), and what is that end type called?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

A sentinel. A C++20 range is an iterator plus a sentinel, any type that can be compared with the iterator, so the end can be a condition such as reaching a null character or a count rather than a known position.

open as a page

In C++17, how do you change the key of an element already stored in a std::map or std::multimap, and what does extract save you?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Stored keys are const, so extract the node, write nh.key(), and insert the node handle back. The element is never copied, moved or reallocated; only tree links change, and a pointer taken before extraction refers to the same element afterwards.

open as a page

When does C++23 std::flat_map beat std::map for an order book's price levels, and what do you give up by switching?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

std::flat_map keeps sorted keys and values in two contiguous containers, so lookup and iteration are cache-friendly and per-element overhead is small. Single-element insert and erase are linear, insertions can invalidate iterators and references, and there are no node handles.

open as a page

In C++23, how does std::mdspan view a flat image buffer as rows and columns, and what do its extents and layout policy decide?

level: seniorimportance: nice to knowfreq 18%

basics

~20 s

std::mdspan is a C++23 non-owning view that maps multidimensional indices onto a flat buffer. Its extents give the rank and each dimension's size, static or dynamic; its layout policy, layout_right by default, computes the offset for img[r, c].

open as a page

showing 31–34 of 34