Containers (Modern C++)

By default, use vector as the default sequential container in C++. This is the equivalent of List<T> in other languages.

vector<widget> v;
v.push_back( “Geddy Lee” );

Use map (not unordered_map) as the default associative container. Use set, multimap, multiset for degenerate & multi cases.

map<string, string> phone_book;
phone_book[“Alex Lifeson”] = “+1 (416) 555-1212”;

When performance optimization is needed, consider using:

  1. the array type when embedding is important, e.g., as a class member.

  2. unordered_map, et al.: Lower per-element overhead (major) and constant-time lookup (minor because O(log N) INVALID USE OF SYMBOLS O(K)). Harder to use correctly and efficiently, because of inconveniences + sharp edges.

  3. Sorted vector. (See: Algorithms.)

Don’t use C arrays. (For older APIs, use f(, vec.size() ); .)

See Also

Other Resources

Welcome Back to C++ (Modern C++)

C++ Language Reference

Standard C++ Library Reference