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 pageshowhide
explore
- Vectors and Arrays6 questions
- Deques, Lists and Adaptors5 questions
- Ordered Maps and Sets6 questions
- Hash Maps and Sets6 questions
- Iterators and Invalidation6 questions
- Span and Mdspan Views5 questions
questions
page 2 of 2In C++20, why may a range's end() return a different type from its begin(), and what is that end type called?
basics
~20 sA 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.
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?
basics
~20 sStored 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.
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?
basics
~20 sstd::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.
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?
basics
~20 sstd::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].
showing 31–34 of 34