Containers (Modern C++)

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

vector<string> apples;
apples.push_back("Granny Smith");

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

map<string, string> apple_color;
// ...
apple_color["Granny Smith"] = "Green";

When performance optimization is needed, consider using:

  • The array type when embedding is important, for example, as a class member.

  • Unordered associative containers such as unordered_map. These have lower per-element overhead and constant-time lookup, but they can be harder to use correctly and efficiently.

  • Sorted vector. For more information, see Algorithms.

Don’t use C-style arrays. For older APIs that need direct access to the data, use accessor methods such as f(vec.data(), vec.size()); instead.

For more information about containers, see C++ Standard Library Containers.

See also

Welcome Back to C++ (Modern C++)
C++ Language Reference
C++ Standard Library