-
Make judicious use of the containers and algorithms of the standard library. The STL is your friend. Do not reinvent the wheel.
-
Prefer using
std::vector<>overstd::list<>,std::deque<>, or any of the heap-based associative containers such asstd::map<>orstd::unordered_map<>in a performance-sensitive context.The node-based structure of these containers make them fairly cache un-friendly, so performance considerations should be carefully balanced against their ease of use.
std::vector<>on the other hand provides great cache locality, since its data storage is guaranteed to be contiguous. -
Strongly prefer algorithms over hand-written loops. Use STL algorithms wherever appropriate.
- STL algorithms (headers
<algorithm>and<numeric>) clearly express intent by their function name, and contain optimized implementations of their respective functionality. Using hand-written loops instead of an appropriate algorithm increases the risk of introducing bugs (due to reinventing the wheel, which is always bad).
- STL algorithms (headers