| title | Containers (Modern C++) | Microsoft Docs | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ms.custom | ||||||||||
| ms.date | 11/04/2016 | |||||||||
| ms.reviewer | ||||||||||
| ms.suite | ||||||||||
| ms.technology |
|
|||||||||
| ms.tgt_pltfrm | ||||||||||
| ms.topic | article | |||||||||
| dev_langs |
|
|||||||||
| ms.assetid | 6e10b758-e928-4827-9c3f-86cafe54bf5b | |||||||||
| caps.latest.revision | 10 | |||||||||
| author | mikeblome | |||||||||
| ms.author | mblome | |||||||||
| manager | ghogen | |||||||||
| translation.priority.ht |
|
|||||||||
| translation.priority.mt |
|
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]((../standard-library/unordered-map-class.md). 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.
Welcome Back to C++
C++ Language Reference
C++ Standard Library