This is a series of blog posts to help anyone preparing for any interview to revise the important concepts of competitive programming in cpp quickly with resources from web that I found helped me the best.
This post covers the commonly used data structure libraries in cpp-
The containers can be divided into 4 main categories:
- Sequence containers- elements are accessed sequentially
- arrays
- vector
- list
- forward_list
- Contaner adaptors- interface on sequential containers
- stack (LIFO)
- queue (FIFO)
- priority_queue (first element is greatest)
- Associative containers- ordered data structures that can be quickly searched
- Unordered associative containers- unordered data structures that can be quickly searched
- unordered_set
- unordered_multiset
- unordered_map
- unordered_multipmap
Below is a quick reference to methods which are frequently used. This is not an exhaustive list and objective of this is to only provide a quick recap before you start solving questions again after a long gap. For a more detailed understanding refer this.
Vector
- This is a dynamic array that automatically resizes itself.
- Data is inserted at the end. Hence, removing the last element takes constant time. Inserting and erasing at the beginning or in the middle is linear in time.
- Commonly used methods on vectors-
begin()
- iterator to the startend()
- iterator to the last elementsize()
- number of elements in vectorempty()
- boolean value indicating if vector has zero elementsfront()
- returns the first elementback()
- returns the last elementpush_back(value)
- push new element to vectorpop_back()
- removes the last elementinsert(iterator position, value)
- insert value at position specified (position should be an iterator- for 5th positon, v.begin() + 5)insert(iterator position, size, value)
- insert value number of times as defined by sizeinsert(iterator position, iterator iterator1, iterator iterator2)
- insert vector in another vectorclear()
- remove all elementserase(iterator position)
- remove element from positionerase(iterator start, iterator end)
- remove elements between starting and ending iteratorsat(position)
- fetches the element at positionswap(vectorname)
- swap contents of vector v1 and v2emplace(iterator position, element)
- insert element at positionemplace_back(value)
- add value at the end of vector
- emplace / emplace_back are in-place while insert / push_back are not
- sort v in asc-
sort(v.begin(), v.end(), greater<int>())
- sort v in desc -
sort(v.begin(), v.end(), greater<int>())
- initialise vector of size 2 to 0 -
vector<int> a(26,0)
Stack
- This is based on LIFO (Last element in, first to pop out)
- Decalared as stack
- Commonly used methods on stack-
top()
- returns element at the top of the stackpop()
- removes the top elementpush(value)
- adds value to top of the stacksize()
- number of elementsempty()
- boolean value indicating if stack has no elements
Queue
- This is based on LIFO (first element in, first to pop out)
- Decalared as queue
- Commonly used methods on queue-
front()
andback()
- returns first and last element respectivelypop()
- deletes first elementpush(value)
- add value to end of queuesize()
- number of elementsempty()
- boolean value indicating if queue has no elements
Priority Queue
- Syntax to create max heap (default) -
priority_queue<int>
- Syntax to create min heap -
priority_queue<int, vector<int>, greater<int>>
In general, the syntax follows - priority_queue<data_type, store<data_type>, comparator>
. Say to define min heap for vector, we define as-
priority_queue<vector<int>, vector<vector<int>>, compare> pq;
struct compare {
bool operator()(vector<int> &v1, vector<int> &v2) {
// define comparison condition
return v1[0] + v1[1] < v2[0] + v2[1];
}
}
- Commonly used methods on priority queue(pq)-
top()
- returns first elementpop()
- deletes first elementpush(value)
- add value to end of queuesize()
- number of elementsempty()
- boolean value indicating if pq has no elements
Set
- In this container, each elemnt is unqiue.
- The value once added cannot be modified. The value can be removed though and updated value can be added.
- Elements are iterated in sorted order always (asc). To iterate in dsc order instead, use <greater
> at the time of initialisation. - Commonly used methods on set-
begin()
- iterator to the start of the vectorend()
- iterator to the last elementsize()
- number of elements in vectorempty()
- boolean value indicating if set has zero elementsinsert(value)
- add value to setinsert(iterator position, value)
- add value at positioninsert(iterator start, iterator end)
- elements between the range [start,end) are inserted in the set from s1 to s2erase(iterator position)
- remove element from positionerase(value)
- remove value from setclear()
- remove all elementsfind(value)
- returns iterator to value or iterator to end if not foundcount(value)
- returns 0 / 1 based on value present in set or notlower_bound(key)
- returns iterator for value less than key, if not present, the iterator to immediate next greater is returnedupper_bound(key)
- returns iterator for value greater than keyemplace(value)
- add value to set
- emplace is in-place operation
set<pair<char, int>> s; // declaring set s.emplace('a', 24); // using emplace() to insert pair in-place s.insert(make_pair('b', 25)); // uses extra space
Read pair
Map
- Each element has a <key, value>. No two mapped values can have same key values.
- Commonly used methods on map-
begin()
- iterator to the start of the vectorend()
- iterator to the last elementerase(iterator position)
anderase(key)
- deletes element at position or key respectivelysize()
- number of elementsempty()
- boolean value indicating if pq has no elementsclear()
- deletes all elements from mapinsert({key, value})
- add key,value to map
- When iterating using an iterator object (say, begin() or end()) - use
itr->first
oritr->second
, otherwise use operator[]
to access elements
Unordered Map
- Stored key, value where key is not sorted in any order unlike map
- Commonly used methods on unordered map-
begin()
- iterator to the start of the vectorend()
- iterator to the last elementat(key)
- returns the value for the keycount(key)
- returns true/ false indicating if value is presentsize()
- number of elementsempty()
- boolean value indicating if um has no elements
- Difference between at() and [] - at() throws exception if key is not present, whereas [] will first insert they key into the map and assign default value (say, integer 0).
Pair
- This is defined in the header
library. (WIP)