12.9 Advice
[1] An STL container defines a sequence; §12.2.
[2] STL containers are resource handles; §12.2, §12.3, §12.5, §12.6.
[3] Use vector as your default container; §12.2, §12.8; [CG: SL.con.2].
[4] For simple traversals of a container, use a range-for loop or a begin/end pair of iterators; §12.2, §12.3.
[5] Use reserve() to avoid invalidating pointers and iterators to elements; §12.2.
[6] Don’t assume performance benefits from reserve() without measurement; §12.2.
[7] Use push_back() or resize() on a container rather than realloc() on an array; §12.2.
[8] Don’t use iterators into a resized vector; §12.2 [CG: ES.65].
[9] Do not assume that [ ] range checks; §12.2.
[10] Use at() when you need guaranteed range checks; §12.2; [CG: SL.con.3].
[11] Use range-for and standard-library algorithms for cost-free avoidance of range errors; §12.2.2.
[12] Elements are copied into a container; §12.2.1.
[13] To preserve polymorphic behavior of elements, store pointers (built-in or user-defined); §12.2.1.
[14] Insertion operations, such as insert() and push_back(), are often surprisingly efficient on a vector; §12.3.
[15] Use forward_list for sequences that are usually empty; §12.8.
[16] When it comes to performance, don’t trust your intuition: measure; §12.2.
[17] A map is usually implemented as a red-black tree; §12.5.
[18] An unordered_map is a hash table; §12.6.
[19] Pass a container by reference and return a container by value; §12.2.
[20] For a container, use the ()-initializer syntax for sizes and the {}-initializer syntax for sequences of elements; §5.2.3, §12.2.
[21] Prefer compact and contiguous data structures; §12.3.
[22] A list is relatively expensive to traverse; §12.3.
[23] Use unordered containers if you need fast lookup for large amounts of data; §12.6.
[24] Use ordered containers (e.g., map and set) if you need to iterate over their elements in order; §12.5.
[25] Use unordered containers (e.g., unordered_map) for element types with no natural order (i.e., no reasonable <); §12.5.
[26] Use associative containers (e.g., map and list) when you need pointers to elements to be stable as the size of the container changes; §12.8.
[27] Experiment to check that you have an acceptable hash function; §12.6.
[28] A hash function obtained by combining standard hash functions for elements using the exclusive-or operator (^) is often good; §12.6.
[29] Know your standard-library containers and prefer them to handcrafted data structures; §12.8.
[30] If your application is suffering performance problems related to memory, minimize free store use and/or consider using a specialized allocator; §12.7.