cd /news/developer-tools/c-standard-template-library-stl-cont… · home topics developer-tools article
[ARTICLE · art-13102] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

C++ Standard Template Library (STL) containers

The C++ STL containers are categorized into sequence containers (vector, deque, list) and associative containers (set, map), each with specific iterator capabilities and time complexities for operations. Sequence containers support front and back insertion/removal in O(1) time, while associative containers use self-balancing binary search trees for O(log n) search, insert, and remove operations. Container adaptors like stack, queue, and priority_queue provide restricted interfaces built on underlying containers such as deque or vector.

read2 min views18 publishedSep 28, 2013
###Containers Forward Container: forward iterators - increment only in O(1)
- begin(), end()
  • All containers
Reversible Container: bidirectional iterators - increment and decrement in O(1)
- rbegin(), rend()
  • vector, deque, list Random Access Container: bidirectional iterators with O(1) forward and backward
  • operator[]
  • vector, deque
###Sequence Containers (vector, deque, list) Front Sequence: insert, remove in beginning in O(1)
- front(), push_front(), pop_front()
  • deque, list
Back Sequence: insert, remove at end in O(1)
- back(), push_back(), pop_back()
  • vector, deque, list Note about deques: Unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations, thus not allowing direct access by offsetting pointers to elements. Elements of a deque can be scattered in different chunks of storage, with the container keeping the necessary information internally to provide direct access to any of its elements in constant time and with a uniform sequential interface. Little more complex internally than vectors, but grows more efficiently under certain circumstances, especially with very long sequences, where reallocations become more expensive.
For operations that involve frequent insertion or removals of elements at positions other than the beginning or the end, deques perform worse and have less consistent iterators and references than lists
###Associate Containers (set, map)

Sets and Maps are implemented with self-balancing BST (red/black). Search, insert, remove takes O(log n). Full iteration is O(n). Copy is O(n log n). Simple Associate Container: elements are their own keys.

  • set, multiset, hash_set, hash_multiset Pair Associate Container: Associates a key with some other item.
  • map, multimap, hash_map, hash_multimap Unique Associate Container: Keys are unique
  • set, map, hash_set, hash_map Multiple Associate Container: Duplicate keys are possible
  • multiset, multimap, hash_multiset, hash_multimap ###Container Adaptors stack (LIFO): Underlying container is deque.
- O(1) operations: push() [front], pop() [front], top()
queue (FIFO): Underlying container is deque.
- O(1) operations: push() [back], pop() [front], front()

Priority_queue: Underlying container is vector and maintained as a max-heap with make_heap().

  • O(log n) operations: push() [insert], pop() [front], top() [largest elem]
── more in #developer-tools 4 stories · sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/c-standard-template-…] indexed:0 read:2min 2013-09-28 ·