c++11 containers
sorted_vector use when doing lots of unsorted insertions and maintaining constant sort would be expensive
map sorted binary search tree; always sorted by key; you can walk through in sorted order
multimap same as map but allows dupe keys; need for this should be pretty rare
unordered_map hashmap; always sorted by key; additional bucket required for hash collisions; no defined order when walking through
unordered_multimap same as map but allows dupe keys; dupes are obviously in the same bucket, and you can walk just the dupes if needed
sets are just like maps, except the key is embedded in the object, great idea
but… they are ruined by the constraint that contained items must be const
but… then redeemed by mutable
You can use mutable on the variable that are not part of the key to remove the const!
This changes the constness of the object from binary (completely const) to logical (constness is defined by the developer)
so… set is an excellent way to achieve both encapsulation and logical const

Final note: using object pointers is the ultimate solution.
The entire object can be dereferenced and accessed then without const issues.
Two requirements: you must make sure yourself that you do not change the key values;
you must create sort functors that dereference the pointers to sort using object contents if needed
(the default sorting will be by pointer address).
The arguably biggest advantage, as a result, is that you can create multiple sets
to reference the same group of objects with different sort funtors to create multiple indices.

Leave a Reply