Here are the ones I know of:
- Traditional a.k.a. plain a.k.a. C array
vector
unique_ptr
with an array-type template argumentarray
valarray
dynarray
static_vector
(see also here)small_vector
stable_vector
There are also “variable-length arrays” and “arrays with runtime bounds”, which do not exist in C++; the former exists in C. See also this question and this answer about these non-C++ containers.
Let’s compare the features of all of these. If the container you want doesn’t match one of the lines below exactly, you’ll need to implement your own and choose a name for it.
Criterion | C array | array | vector | unique_ptr | valarray | dynarray | static_vector | small_vector | stable_vector |
---|---|---|---|---|---|---|---|---|---|
Origin/library | language | std | std | std | std | discarded std | Boost | Boost | Boost |
Type parameters | T,N | T,N | T,A | T | T | T | T,C,O | T,N,A,O | T,A |
Capacity fix time | Compile | Compile | Never | (Construct) | Never | Construct | Compile | Never | Never |
Size fix time | Compile | Compile | Never | N/A | Never | Never | Construct | Never | Never |
Size = capacity always? | ✔ | ✔ | ✕ | N/A | ✕ | ✔ | ✕ | ✕ | ✕ |
Storage typically on | Stack | Stack | Heap | Heap | Heap | Heap | Stack | Stack/Heap | Heap |
Stable iterators? | N/A | N/A | ✕ | N/A | N/A | N/A | (✔) | ✕ | ✔ |
Constraint on element type | ✕ | ✕ | ✕ | ✕ | ✔ | ✕ | ✕ | ✕ | ✕ |
Template parameter legend:
- A for allocator
- T for element type
- N for size in number of elements
- C for capacity in number of elements
- O for options
Finally, an interesting but less popular vector-like container is the “veque”, or deque-vector amalgam.
4
solved What variations of vector-like containers already widely established? Do I have to write my own? [closed]