| 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 default sequential container in C++. This is the equivalent of List<T> in other languages.
vector<string> 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:
-
the array type when embedding is important - for example, as a class member.
-
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.
-
sorted vector. (See: Algorithms.)
Don’t use C arrays. (For older APIs, use f( vec.data(), vec.size() ); .)
For another article about containers, see STL Containers.
The following tables show the container sizes, in bytes, for x86 and x64 platforms. (For these purposes, 32-bit ARM is equivalent to x86.) These tables cover release mode, because debug mode contains checking machinery that consumes space and time. The separate columns are for [!INCLUDEcpp_orcas_long] SP1, where _SECURE_SCL defaulted to 1, and for [!INCLUDEcpp_orcas_long] SP1 with _SECURE_SCL manually set to 0 for maximum speed. Visual C++ in Visual Studio 2010, [!INCLUDEcpp_dev11_long], and [!INCLUDEcpp_dev12_long] default _SECURE_SCL to 0 (now known as _ITERATOR_DEBUG_LEVEL).
| x86 Container Sizes (Bytes) | VC9 SP1 | VC9 SP1 SCL=0 |
VC10 | VC11 |
|---|---|---|---|---|
| vector<int> | 24 | 16 | 16 | 12 |
| array<int, 5> | 20 | 20 | 20 | 20 |
| deque<int> | 32 | 32 | 24 | 20 |
| forward_list<int> | N/A | N/A | 8 | 4 |
| list<int> | 28 | 12 | 12 | 8 |
| priority_queue<int> | 28 | 20 | 20 | 16 |
| queue<int> | 32 | 32 | 24 | 20 |
| stack<int> | 32 | 32 | 24 | 20 |
| pair<int, int> | 8 | 8 | 8 | 8 |
| tuple<int, int, int> | 16 | 16 | 16 | 12 |
| map<int, int> | 32 | 12 | 16 | 8 |
| multimap<int, int> | 32 | 12 | 16 | 8 |
| set<int> | 32 | 12 | 16 | 8 |
| multiset<int> | 32 | 12 | 16 | 8 |
| hash_map<int, int> | 72 | 44 | 44 | 32 |
| hash_multimap<int, int> | 72 | 44 | 44 | 32 |
| hash_set<int> | 72 | 44 | 44 | 32 |
| hash_multiset<int> | 72 | 44 | 44 | 32 |
| unordered_map<int, int> | 72 | 44 | 44 | 32 |
| unordered_multimap<int, int> | 72 | 44 | 44 | 32 |
| unordered_set<int> | 72 | 44 | 44 | 32 |
| ordered_multiset<int> | 72 | 44 | 44 | 32 |
| string | 28 | 28 | 28 | 24 |
| wstring | 28 | 28 | 28 | 24 |
| x64 Container Sizes (Bytes) | VC9 SP1 | VC9 SP1 SCL=0 |
VC10 | VC11 |
|---|---|---|---|---|
| vector<int> | 48 | 32 | 32 | 24 |
| array<int, 5> | 20 | 20 | 20 | 20 |
| deque<int> | 64 | 64 | 48 | 40 |
| forward_list<int> | N/A | N/A | 16 | 8 |
| list<int> | 56 | 24 | 24 | 16 |
| priority_queue<int> | 56 | 40 | 40 | 32 |
| queue<int> | 64 | 64 | 48 | 40 |
| stack<int> | 64 | 64 | 48 | 40 |
| pair<int, int> | 8 | 8 | 8 | 8 |
| tuple<int, int, int> | 16 | 16 | 16 | 12 |
| map<int, int> | 64 | 24 | 32 | 16 |
| multimap<int, int> | 64 | 24 | 32 | 16 |
| set<int> | 64 | 24 | 32 | 16 |
| multiset<int> | 64 | 24 | 32 | 16 |
| hash_map<int, int> | 144 | 88 | 88 | 64 |
| hash_multimap<int, int> | 144 | 88 | 88 | 64 |
| hash_set<int> | 144 | 88 | 88 | 64 |
| hash_multiset<int> | 144 | 88 | 88 | 64 |
| unordered_map<int, int> | 144 | 88 | 88 | 64 |
| unordered_multimap<int, int> | 144 | 88 | 88 | 64 |
| unordered_set<int> | 144 | 88 | 88 | 64 |
| ordered_multiset<int> | 144 | 88 | 88 | 64 |
| string | 40 | 40 | 40 | 32 |
| wstring | 40 | 40 | 40 | 32 |
Welcome Back to C++
C++ Language Reference
C++ Standard Library