Partager via


Containers (Modern C++)

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Containers (Modern C++). By default, use vector as the preferred sequential container in C++. This is the equivalent of 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, multiset for degenerate & multi cases.

map<string, string> apple_color;  
// ...  
apple_color["Granny Smith"] = "Green";  
  1. the array type when embedding is important - for example, as a class member.

  2. unordered associative containers (unordered_map, et al.): Lower per-element overhead (major) and constant-time lookup (potentially major, sometimes minor). Harder to use correctly and efficiently, because of inconveniences and sharp edges.

  3. sorted vector. (See: Algorithms.)

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

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

See Also

Welcome Back to C++
C++ Language Reference
C++ Standard Library Reference