![]() |
VOOZH | about |
STL provides a range of data structures that are very useful in various scenarios. A lot of data structures are based on real-life applications. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized.
Containers or container classes store objects and data. There are in total seven standards "first-class" container classes and three container adaptor classes and only seven header files that provide access to these containers or container adaptors.
Note: We can include just one library, i.e., #include <bits/stdc++.h> that includes all the STL libraries but in certain competitions, including this library can make the code slow. To overcome this problem, we can add specific libraries to access particular data structures of STL. Also while removing the elements, it is required to take care if the data structure is empty or not. Calling remove function on an empty data structure leads to error. Below are the some Data Structures with their illustration shown
Header file:
#include <vector>
Syntax:
vector<data type> variable_name;
Most common function for vector:
Initial vector: 1 2 3 4 5 6 7 8 9 10 Vector size: 10 Is vector is empty: False Vector after popping: 1 2 3 4 5 6 7 8 9 Vector after erase first element: 2 3 4 5 6 7 8 9 Vector after clearing: None Is vector is empty: True
2. Stack: It is Last In First Out (LIFO) data structure. It can be implemented using arrays, linked lists, and vectors. Some problems like reversing an element or string, parenthesis check, print next greater element, postfix expression, etc, can be done using stack class rather than making all the functions we can use its inbuilt functions.
Header file:
#include <stack>
Syntax:
stack<data_type> variable_name;
Most common function for stack:
Stack is currently Empty Size of stack: 13 Stack initially: G E E K S F O R G E E K S Top element: G Stack after 1pop operation: E E K S F O R G E E K S Top element after popping: E Size of stackafter popping: 12 Stack is not empty
3.Queue: It is First In First Out (FIFO) data structure.The reason why we require queues use a lot of practical application of first in first out and when data doesn't need to be processed early. For example, in a queue for buying tickets for a show, the one who enters the queue first, get the ticket first. It can be implemented using arrays, linked lists, and vectors just like stacks. Some applications of queue include level-order traversal in trees and graphs, in resource sharing, etc.
Header file:
#include <queue>
Syntax:
queue<Data Type> variable_name;
Most common function for Queue:
Queue is empty Queue Initially: G E E Front element: G Back Element: S Size of queue: 5 Queue is not empty
4.Priority Queue: This data structure is similar to queues, but the order of first out is decided by a priority set by the user. The main functions include getting top priority element, insert, delete the top priority element or decrease the priority. Data structure Heaps is used and not BST, as in BST, creating trees is costly than heaps, and the complexity of heaps is better. Also, heaps provide Complete Binary Tree and Heap order property satisfying all the properties of Priority Queue. Priority Queue has 2 variations, Min Heap and Max Heap.
Complex problems like finding k largest or smallest elements, merge k unsorted arrays, Dijkstra’s Shortest Path Algorithm, Huffman code for compression, Prim's Algorithm, etc. can be implemented easily.
Header file:
#include <queue>
Syntax:
Min Priority Queue: priority_queue<Data Type> variable_name;
Max Priority Queue: priority_queue <Data Type, vector, greater> variable_name;
Most common function for priority queue:
MIN priority queue is empty MAX priority queue is empty MIN priority queue: 1 2 3 4 5 6 7 8 9 10 MAX priority queue: 10 9 8 7 6 5 4 3 2 1 Size of min pq: 10 Size of max pq: 10 Top of min pq: 1 Top of max pq: 10 MIN priority queue after pop: 2 3 4 5 6 7 8 9 10 MAX priority queue after pop: 9 8 7 6 5 4 3 2 1 Size of min pq: 9 Size of max pq: 9 MIN priority queue is not empty MAX priority queue is not empty
4.Set: Sets are associative containers in which each element is unique. The elements cannot be modified once inserted in the set. A set ignores the duplicate values and all the elements are stored in sorted order. This data structure is particularly useful when incoming elements need to be sorted and modification is not required.
The sets can store elements in two orders, increasing order or decreasing order.
Header file:
#include <set>
Syntax:
Increasing order: set<Data type> variable_name;
Decreasing order :set<Data type, greater<Data type> > variable_name;
Most common function for Set:
5.List: Lists stores data in non-contiguous manner.Elements of the list can be scattered in the different chunks of memory.Access to any particular index becomes costly as traversal from know index to that particular index has to be done, hence it is slow than vector.Zero-sized lists are also valid.
Header file:
#include <list>
Syntax:
list<Data Type> variable_name;
Most common function for list:
6.Unordered Maps: Suppose you have a pencil box and some pens. You put some pens in the box, that would be random and not ordered unlike ordered maps but you can access any pen and work with it. The same is unordered maps, the elements are stored randomly but you can access any order anytime. The main difference is between ordered maps and unordered maps are the way the compiler stores them. It has two arguments, the first is called KEY, and the other is called Value. The keymaps to the value and is always unique.
Header file:
#include <unordered_map>
Syntax:
unordered_map<Data Type> variable name;
Most common function for unordered map:
7.Ordered Maps: Suppose you have a new shelf in your room and some books. You arrange the books one after another but after arranging any number of books, you can access arranged books in any order and keep the book in the same place when you've read it. This is an example of a map. You fill the values one after another in order and the order is maintained always, but you can access any element anytime. This data structure is also implemented using dynamic arrays. Like unordered maps, it has two arguments, the first is called KEY, and the other is called Value. The keymaps to the value and is always unique.
Header file:
#include <ordered_map>
Syntax:
ordered_map<Data Type> variable name;
Most common function for ordered map:
1.) Pair : The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second). Pair is used to combine together two values which may be different in type. Pair provides a way to store two heterogeneous objects as a single unit. Pair can be assigned, copied and compared. The array of objects allocated in a map or hash_map are of type ‘pair’ by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.To access the elements, we use variable name followed by dot operator followed by the keyword first or second.
Header File :
#include <utility>
Syntax :
pair (data_type1, data_type2) Pair_name;
Most common function for Pair :
Below is the code to implement the above function : -
If you have an array of pair and you use inbuilt sort function then , by default it sort the array on the basis of first value(i.e. obj.first) of the array of each element .To sort in Descending order pass the function according to which you want to sort the array.
Below Code will show how to sort the Array of pair : -
Ordered Set : Ordered set is a policy based data structure in g++ that keeps the unique elements in sorted order. It performs all the operations as performed by the set data structure in STL in log(n) complexity and performstwo additional operations also in log(n) complexity .
Header File and namespace :-
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
The necessary structure required for the ordered set implementation is :
tree < int , null_type , less , rb_tree_tag , tree_order_statistics_node_update >
You can read about them in detail here .
Additional functions in the ordered set other than the set are -
Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
*(s.find_by_order(2)) : 3rd element in the set i.e. 6
*(s.find_by_order(4)) : 5th element in the set i.e. 88
2. order_of_key(k) : It returns to the number of items that are strictly smaller than our item k in O(log n) time.
Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.
s.order_of_key(25) : Count of elements strictly smaller than 25 is 4.
NOTE : ordered_set is used here as a macro given to
tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update>.
Therefore it can be given any name as macro other than ordered_set
but generally in the world of competitive programming it is commonly referred as ordered set
as it is a set with additional operations.
2 2 2 5 1