Files
2026-05-04 08:12:20 +08:00

3.0 KiB

C++ Containers

Container Concept: Common Interface   containers concept

Front

What interface is shared by all C++ standard containers?

Back

  • Element access: at, operator[], front, back, data
  • Iterators: begin, end, cbegin, cend, rbegin, rend
  • Capacity: empty, size, max_size
  • Modifiers: clear, insert, erase, push_back, pop_back, resize, swap

Do C++ containers inherit from a common base?   containers concept inheritance

Front

Do C++ containers (vector, list, map, etc.) inherit from a common base class?

Back

No. Standard containers are standalone classes. Shared interfaces come from conventions and C++20 concepts (Container, SequenceContainer), not inheritance.

Sequence Containers   containers sequence

Front

What are the C++ sequence containers?

Back

vector, deque, list, forward_list, array

Sequence containers maintain insertion order.

Associative Containers   containers associative

Front

What are the C++ associative containers?

Back

  • Ordered: set, multiset, map, multimap
  • Unordered: unordered_set, unordered_multiset, unordered_map, unordered_multimap

Container Adapters   containers adapter

:PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 20260504006

Front

What are the C++ container adapters?

Back

stack, queue, priority_queue

Adapters provide a restricted interface (e.g., LIFO for stack, FIFO for queue).

What does vector have that other containers don't?   containers vector

Front

Which methods does vector have that other containers typically don't?

Back

reserve, capacity, shrink_to_fit

These manage the internal memory buffer.

Unique methods on sequence containers (list, deque)   containers sequence

Front

Which sequence containers support push_front and pop_front?

Back

deque and list (not vector)

These containers support insertion/removal at both ends.

Unique methods on associative containers   containers associative

Front

What tree-based methods do ordered associative containers have?

Back

find, count, lower_bound, upper_bound, equal_range

These use tree traversal (BST behind the scenes).

Unique methods on unordered containers   containers unordered

Front

What hash-specific methods do unordered containers have?

Back

bucket_count, load_factor, max_load_factor, rehash, reserve

These manage hash table internals.