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");
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.
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.