STL Containers¶
The Standard Template Library (STL) offers a suite of highly optimized container templates. Choosing the correct container for a given workload is essential for maximizing performance, especially on resource-constrained platforms like the Raspberry Pi.
1. Sequence Containers¶
Sequence containers store elements in a linear arrangement.
1. std::vector<T> (The Default Standard)¶
std::vector represents a dynamic array. Elements are stored in contiguous memory blocks.
- Random Access: $O(1)$ constant time.
- Insertion at end: Amortized $O(1)$.
- Insertion in middle: $O(n)$ linear time (since elements must be shifted in memory).
Capacity vs. Size and the reserve() Optimization¶
When a vector's size exceeds its allocated memory (capacity), it allocates a new, larger memory block (usually $1.5\times$ or $2\times$ the old size), copies the existing elements to the new block, and deletes the old block. This is a very expensive operation.
If you know the approximate number of elements beforehand, use .reserve() to pre-allocate memory and prevent redundant reallocations:
2. std::array<T, N> (Fixed-Size Stack Allocation)¶
std::array is a safe, modern wrapper around raw C-style arrays. Its size must be known at compile time.
- Overhead: Zero runtime overhead compared to raw arrays.
- Safety: Supports .at(index) which performs bound checking (throwing std::out_of_range on failure).
3. std::list<T> (Doubly-Linked List)¶
std::list allocates memory for elements as individual, non-contiguous nodes.
- Insertion/Deletion: $O(1)$ anywhere in the list (once the node is located).
- Access: $O(n)$ search (no index access, must traverse node pointers).
- Cache Misses: High. Contiguous memory containers like std::vector perform significantly better on modern CPUs because of CPU cache lines.
2. Associative & Unordered Containers¶
Associative containers store key-value pairs or unique keys, optimized for fast lookup.
| Container | Implementation | Key Sorting | Search Complexity | Typical Use Case |
|---|---|---|---|---|
std::map |
Balanced Binary Tree (Red-Black) | Yes (Sorted) | $O(\log n)$ | When keys must be sorted |
std::unordered_map |
Hash Table | No (Arbitrary) | $O(1)$ Average / $O(n)$ Worst | Fast dictionary lookups |
std::set |
Balanced Binary Tree | Yes | $O(\log n)$ | Maintaining unique sorted keys |
std::unordered_set |
Hash Table | No | $O(1)$ Average | Fast uniqueness checks |
3. Iterators¶
Iterators behave like generalized pointers, allowing you to traverse different container types using a unified syntax.
4. Modern Non-Owning Views: Zero-Copy Views¶
Modern C++ provides lightweight, non-owning reference templates that point to existing memory buffers. This avoids expensive copying and allocates no heap memory.
1. std::string_view (C++17)¶
Provides a read-only view of a contiguous character sequence (works with std::string or const char*).
2. std::span (C++20)¶
Provides a view of a contiguous block of objects. It can wrap a raw C-style array, a std::vector, or a std::array without making a copy.
Related Guides¶
- Templates — How STL containers are built using generic blueprints.
- STL Algorithms — Iterating and manipulating data inside STL containers.