Ch10: Associative Containers
Overview
A sequential container (Chapter 9) stores elements at the positions where you put them: index 0, index 1, and so on. An associative container does not care about positions. You give it a key, and it finds the element again later, without you remembering where it went.
The containers in this chapter all use a string key. This
is the case that matters in real programs: counting words, mapping names
to records, looking up settings, caching results, checking membership of a
token. fast_io gives you two families for it, each in a
set flavour (the key is the whole element) and a map
flavour (the key is attached to a value):
| Family | Set | Map | Order of elements |
|---|---|---|---|
| B-tree | ::fast_io::str_btree_set |
::fast_io::str_btree_map<M> |
Always sorted by key. |
| Ranked B-tree | ::fast_io::str_ranked_btree_set |
::fast_io::str_ranked_btree_map<M> |
Sorted by key, and knows how many elements come before each one. |
| Swiss table (hash) | ::fast_io::str_swiss_set |
::fast_io::str_swiss_map<M> |
Unordered — the order is an artefact of the hashing. |
All six types share the same style of interface, so what you learn for one
mostly carries over to the others: insert_key,
contains, find, erase_key, and
iteration with begin()/end(). The differences are
what each family can do beyond that (order, rank, capacity) and how much
bookkeeping it does per element.
"alpha", the container keeps its own null-terminated copy.
When you look a key up, you only pass a view
(::fast_io::string_view / ::fast_io::cstring_view),
so a lookup never allocates. This is covered in
Ch10.1.
Contents
- Ch10.1: What an Associative Container Is
- Ch10.2: Creating a Container and Inserting Keys
- Ch10.3: contains(): Membership Testing
- Ch10.4: Looking Keys Up
- Ch10.5: Iterating
- Ch10.6: Erasing
- Ch10.7: Maps: Keys With Values
- Ch10.8: Ranked B-trees: size() and nth_element()
- Ch10.9: Swiss Tables: Unordered and Resizable
- Ch10.10: Choosing a Container
- Ch10.11: Element Types, Error Specifications, and Validity