{"slug": "c-standard-template-library-stl-containers", "title": "C++ Standard Template Library (STL) containers", "summary": "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.", "body_md": "###Containers Forward Container: forward iterators - increment only in O(1)\n- begin(), end()\n- All containers\nReversible Container: bidirectional iterators - increment and decrement in O(1)\n- rbegin(), rend()\n- vector, deque, list\nRandom Access Container: bidirectional iterators with O(1) forward and backward\n- operator[]\n- vector, deque\n###Sequence Containers (vector, deque, list) Front Sequence: insert, remove in beginning in O(1)\n- front(), push_front(), pop_front()\n- deque, list\nBack Sequence: insert, remove at end in O(1)\n- back(), push_back(), pop_back()\n- vector, deque, list\nNote 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.\nElements 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.\nFor 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\n###Associate Containers (set, map)\nSets and Maps are implemented with self-balancing BST (red/black).\nSearch, insert, remove takes O(log n). Full iteration is O(n). Copy is O(n log n).\nSimple Associate Container: elements are their own keys.\n- set, multiset, hash_set, hash_multiset\nPair Associate Container: Associates a key with some other item.\n- map, multimap, hash_map, hash_multimap\nUnique Associate Container: Keys are unique\n- set, map, hash_set, hash_map\nMultiple Associate Container: Duplicate keys are possible\n- multiset, multimap, hash_multiset, hash_multimap\n###Container Adaptors stack (LIFO): Underlying container is deque.\n- O(1) operations: push() [front], pop() [front], top()\nqueue (FIFO): Underlying container is deque.\n- O(1) operations: push() [back], pop() [front], front()\nPriority_queue: Underlying container is vector and maintained as a max-heap with make_heap().\n- O(log n) operations: push() [insert], pop() [front], top() [largest elem]", "url": "https://wpnews.pro/news/c-standard-template-library-stl-containers", "canonical_source": "https://gist.github.com/alyssaq/6743129", "published_at": "2013-09-28 15:27:43+00:00", "updated_at": "2026-05-24 04:35:04.683413+00:00", "lang": "en", "topics": ["developer-tools", "data"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/c-standard-template-library-stl-containers", "markdown": "https://wpnews.pro/news/c-standard-template-library-stl-containers.md", "text": "https://wpnews.pro/news/c-standard-template-library-stl-containers.txt", "jsonld": "https://wpnews.pro/news/c-standard-template-library-stl-containers.jsonld"}}