STL - Sequence Containers & Adaptors
cpp
stl
Sequence Containers
array
- Static arraysvector
- Dynamic arraysforward_list
- Singly linked-listlist
- Doubly linked-listdeque
- Double-ended queue
Common member functions in sequential containers:
.begin()
- Returns an iterator pointing to the start of the container.end()
- Returns an iterator pointing to the address next to the last element in the container.empty()
- Returns whether the container is empty.clear()
- Clear all contents present inside the container.size()
- Returns number of items present inside the container (absent inforward_list
).max_size()
- Returns theoretical maximum number of elements that the container can hold.swap(other)
- Swap content of current container with similarother
container.resize(n)
,resize(n,val)
- Resize container to containn
elements. Can also pass default valueval
to set newly created elements as
Note: The *emplace*
member functions perform insertions a bit faster than *push*
or *insert*
functions as they construct the new element in-place (instead of creating and then copying them)
Adaptors for Sequence Containers
They provide a wrapper interface for Sequential containers
stack
- provides Stack (LIFO)queue
- provides Queue (FIFO)priority_queue
- provides Heap (Max-Heap by deafult)
Singly Linked-List
- Declaration:
forward_list<Type> ll;
- Supports
O(1)
insertion/deletion if location specified (only links updated) - Operations at the start are
O(1)
andO(N)
elsewhere. - Random access NOT present. Only the start (i.e. head) accessible
- Uni-directional access to only forward adjacent element:
next(pos)
- CPP Reference:
forward_list
Element Access:
.front()
- Access first element in the list
Iterators:
.before_begin()
- Returns an iterator pointing to a placeholder element before the first element in the list
- Trying to access it results in undefined behaviour
- Generally used with:
.insert_after()
,.emplace_after()
,.erase_after()
etc. - Incrementing this iterator gives
.begin()
Note: The list goes in only one direction i.e. forward. So, we can only do itr++
, but NOT itr--
(where itr
is an iterator pointing to some element in the list)
Capacity: Note that .size()
function is NOT provided
Modifiers:
.pop_front()
- Remove first element from the list.push_front(item)
,.emplace_front(item)
- Insert elementitem
at start of the list.insert_after()
: Insertion takesO(1)
per entry in these operations.insert_after(pos,val)
- Insert elementitem
after the positionpos
.insert_after(pos,count,item)
- Insertcount
copies ofitem
after positionpos
.insert(pos,start,end)
- Insert all elements within[start,end)
after positionpos
.emplace_after(pos,...args)
- Construct element in-place after positionpos
forward_list
demo
Doubly-Linked List
- Declaration:
list<Type> dll;
- Supports
O(1)
insertion/deletion if location specified (only links updated) - Operations at the start and end are
O(1)
, butO(N)
elsewhere - Random access NOT present. Only the start and end is accessible
- Bidirectional access to both forward & backward adjacent elements:
next(pos)
,prev(pos)
- CPP Reference:
list
Element Access:
.front()
- Returns reference to first element in the list.back()
- Returns reference to last element in the list
Iterators: .begin()
, .end()
, .rbegin()
, .rend()
Note: The list goes in both directions. So, we can only do itr++
as well as itr--
(where itr
is an iterator pointing to some element in the list)
Capacity: .size()
function provided
Modifiers:
.push_front(item)
,.emplace_front(item)
- Insert elementitem
at start of the list.push_back(item)
,.emplace_back(item)
- Insert elementitem
at end of the list.pop_front()
- Remove first element from the list.pop_back()
- Remove last element from the list.insert()
: Insertion takesO(1)
per entry in these operations.insert(pos,item)
- Insert elementitem
at positionpos
.insert(pos,count,item)
- Insertcount
copies ofitem
at positionpos
.insert(pos,start,end)
- Insert all elements within[start,end)
at positionpos
.emplace(pos,...args)
- Construct element in-place at positionpos
.erase(pos)
- Removes element atpos
position
list
demo
Double-ended Queue
- Declaration:
deque<Type> dq;
- Operations at the start and end are
O(1)
, butO(N)
elsewhere - Random access is supported in
O(1)
time - How is
deque
different fromvector
?- Unlike
vector
, the elements are not stored contiguously - Indexed access to
deque
must perform two pointer dereferences, compared to only one in indexed access ofvector
- Expansion is cheaper than expansion of
vector
because it does not involve copying existing elements to new memory location
- Unlike
- See this answer for difference between
deque
andlist
- Most implementations use a sequence of individually allocated fixed-size arrays, with additional bookkeeping. They can also expand/contract as needed on both the ends. They also have a large minimal memory cost for allocating the individual arrays
- CPP Reference:
deque
Element Access:
.at(idx)
- Access element atidx
index, with bounds-checking[idx]
- Access element atidx
index.front()
- Access first element.back()
- Access last element
Iterators: .begin()
, .end()
, .rbegin()
, .rend()
Capacity: .size()
provided, .shrink_to_fit()
(free ununsed space)
Modifiers:
.push_front(item)
,.emplace_front(item)
- Insert elementitem
at start of the list.push_back(item)
,.emplace_back(item)
- Insert elementitem
at end of the list.pop_front()
- Remove first element from the list.pop_back()
- Remove last element from the list.insert()
: Insertion element(s) at given postition.insert(pos,item)
- Insert elementitem
at positionpos
.insert(pos,count,item)
- Insertcount
copies ofitem
at positionpos
.insert(pos,start,end)
- Insert all elements within[start,end)
at positionpos
.emplace(pos,...items)
- Construct element in-place at positionpos
Stack
-
The
stack
adaptor provides a Stack data structure wrapper to the container, which has the property of Last-in First-out i.e. LIFO (also called first-in last-out) -
Supports operations ONLY at the top of Stack, which take
O(1)
time each -
It’s defined as:
-
Declaration:
stack<Type> stk;
-
CPP Reference:
stack
Element Access: .top()
returns reference to first i.e. top-most element
Capacity: .size()
, .empty()
Iterators: NONE provided
Modifiers:
.pop()
- Remove element present at the top.push(val)
- Insert element at the top.emplace(...args)
- Construct element in-place at the top
Queue
-
The
queue
adaptor provides a Queue data structure wrapper to the container, which has the property of First-in First-out i.e. FIFO (also called last-in last-out) -
Insertion occurs at the back and Deletion at the front, both taking
O(1)
time -
It’s defined as:
-
Declaration:
queue<Type> q;
-
CPP Reference:
queue
Element Access:
.front()
- returns reference to first i.e. starting element.back()
- returns reference to last i.e. ending element
Capacity: .size()
, .empty()
Iterators: NONE provided
Modifiers:
.pop()
- Remove the first i.e. starting element.push(val)
- Insert element at the end.emplace(...args)
- Construct element in-place at the end
Heap
-
The
priority_queue
adaptor provides a Heap data structure wrapper to the container -
Fast lookup of highest-priority element in
O(1)
time -
Insertion/Deletions take upto
O(logN)
time -
It’s defined as:
-
The default ordering is to keep the lexicographically larger element at the top i.e. a Max-heap, but you can also provide a custom
Compare
function to determine the ordering -
CPP Reference:
priority_queue
-
Declaration:
- Max-heap:
priority_queue< int > maxHp;
- Min-heap:
priority_queue< int, vector<int>, greater<int> > minHp;
- Max-heap:
Element Access: .top()
returns reference to the highest priority element (stored topmost at the root of Heap tree)
Capacity: .size()
, .empty()
Iterators: NONE provided
Modifiers:
.pop()
- Remove the topmost element from the heap.push(val)
- Insert element into the heap.emplace(...args)
- Construct element in-place and insert into the heap
Note: After each insertion/deletion, to maintain the sorted Heap property, the elements are re-organized internally by the adaptor itself.
Also refer: is_heap()
, make_heap()
, push_heap()
, pop_heap()
, sort_heap()
Can also use keyboard characters ←
, →
, ↔
as arrows in display functions