Container Characteristics
IN what follows, we briefly describe each container, along with the main differences between the different classes within each category.
Sequence Containers
Sequence containers store elements in a linear fashion, where the order of insertion is preserved.
-
std::array
:
- Fixed-size array, known at compile-time.
- Provides bounds-checked access through
.at()
.
- Best when size is fixed and known in advance.
-
std::vector
:
- Dynamic array that can grow in size as needed.
- Supports fast random access and allows resizing.
- Ideal for situations where you expect frequent size changes.
-
std::deque
(Double-ended queue):
- Dynamic array-like container with fast insertion/removal at both ends.
- Unlike
vector
, insertions/removals at the front are also efficient.
- Use when you need both efficient front and back operations.
-
std::list
:
- Doubly linked list, allowing fast insertion and removal anywhere.
- Slower random access compared to
vector
and deque
.
- Use when you need frequent insertions/removals at arbitrary positions.
-
std::forward_list
:
- Singly linked list, offering only one-way traversal.
- Lighter than
std::list
but lacks bidirectional traversal.
- Use for memory efficiency when only forward traversal is required.
Key Differences:
- array
: Fixed size, fast random access.
- vector
: Dynamic, fast random access, efficient back insertions.
- deque
: Fast insertions/removals at both ends.
- list
: Fast insertions/removals anywhere but slower access.
- forward_list
: Lightweight, single-direction traversal.
Associative Containers
Associative containers store elements in a specific order based on their keys, typically using binary search trees (balanced).
-
std::set
:
- Stores unique elements in sorted order.
- Provides logarithmic time for insertion, deletion, and search.
- Use when uniqueness and sorted order are required.
-
std::map
:
- Stores key-value pairs (like a dictionary), with unique keys in sorted order.
- Provides logarithmic time for insertion, deletion, and look-up.
- Use when you need sorted key-value associations.
-
std::multiset
:
- Similar to
set
, but allows duplicate elements.
- Elements are sorted, and duplicates are permitted.
- Use when duplicates are allowed but ordering is important.
-
std::multimap
:
- Similar to
map
, but allows duplicate keys.
- Each key can be associated with multiple values, and keys are sorted.
- Use when you need sorted key-value pairs, but keys can repeat.
Key Differences:
- set
/multiset
: Focus on element uniqueness and order.
- map
/multimap
: Focus on key-value associations, where multimap
allows duplicate keys.
Unordered Associative Containers
These containers store elements based on hash tables, offering fast average-time complexity for search, insert, and delete, but without any guaranteed order.
-
std::unordered_set
:
- Stores unique elements, but in no particular order.
- Average O(1) time complexity for insert, find, and erase operations.
- Use when ordering doesn't matter, but uniqueness does.
-
std::unordered_map
:
- Stores key-value pairs with unique keys, using a hash table.
- Average O(1) look-up, insert, and delete.
- Use when you need key-value pairs but don't care about order.
-
std::unordered_multiset
:
- Similar to
unordered_set
, but allows duplicate elements.
- No order is guaranteed for elements, but fast access is provided.
- Use when duplicates are allowed, and order is irrelevant.
-
std::unordered_multimap
:
- Similar to
unordered_map
, but allows duplicate keys.
- Average O(1) for operations, but keys are not ordered.
- Use when you need key-value pairs with possible duplicate keys and don't care about ordering.
Key Differences:
- unordered_set
/unordered_multiset
: Focus on element uniqueness without ordering.
- unordered_map
/unordered_multimap
: Focus on key-value associations without ordering, and unordered_multimap
allows duplicate keys.
Container Adaptors
Container adaptors provide a restricted interface over sequence containers, tailoring them for specific use-cases like stacks or queues.
-
std::stack
:
- A LIFO (Last In, First Out) data structure.
- Built on top of a
deque
, vector
, or list
.
- Provides push, pop, and top operations.
- Use for operations where you need to process elements in reverse order of insertion.
-
std::queue
:
- A FIFO (First In, First Out) data structure.
- Typically implemented with
deque
or list
.
- Provides push, pop, and front operations.
- Use for typical queue operations, where the first inserted element is processed first.
-
std::priority_queue
:
- A max-heap (by default), where the highest priority element is always at the front.
- Built on a random-access container like
vector
or deque
.
- Provides push, pop, and top operations, where top always gives the largest element.
- Use when you need to retrieve elements based on priority.
Key Differences:
- stack
: LIFO behavior (last-in, first-out).
- queue
: FIFO behavior (first-in, first-out).
- priority_queue
: Retrieves elements by priority, not insertion order.
Summary of Main Differences
- Sequence Containers: Provide linear storage.
array
and vector
support fast random access, while deque
, list
, and forward_list
offer varying insertion/deletion trade-offs.
- Associative Containers: Store elements in sorted order with logarithmic time for operations.
set
/map
enforce uniqueness, while multiset
/multimap
allow duplicates.
- Unordered Associative Containers: Provide fast average O(1) time complexity for operations using hash tables. Do not maintain any specific order.
- Container Adaptors: Specialized containers (stack, queue, priority_queue) offer restricted access patterns tailored for LIFO, FIFO, or priority-based retrieval.
This overview should help clarify when and why to use each container!
Previous Page |
Course Schedule |
Course Content