Posts

All about array STL in c++ for competitive programming( initialization, traversal and methods/functions ) :

array STL Important Points: It is a sequence container that encapsulates fixed size array. Size is needed at compile time. It's rarely used in competitive programming as vector is there which is far more better than array STL. Remember to use the header file #include<array> before using array STL. Advantages of using vector over array: Vector is dynamic in nature so, size automatically increases with insertion of elements.  Array is fixed size, once initialized can't be resized but vector can.  Reserve space can be given for vector, whereas for arrays you cannot give reserved space.   Vectors can store any type of objects, whereas an array can store only homogeneous values.  Multiple objects can be stored in vector.   Elements can be deleted in vector.    Disadvantages of vector over array: Vector occupies more memory. Vector takes more time in accessing elements. Array is more appropriate for storing a fixed no. of elements. Array support efficient random access

All about pair STL in c++ for competitive programming( initialization, traversal and methods/functions ) :

pair STL Important Points: It is a simple utility container which consists of a single pair. A pair contains values (t1 and t2) which may be of different types . It provides a way to store two heterogeneous objects as a single unit. Keyword ' first ' is used to refer to the first value (t1) and keyword ' second ' is used to refer to the second value (t2) . A pair can be copied and can be compared with other pairs also. Remember to use the header file #include<utility> before using pair STL.   map and pair: map STL uses pair only to store (key, value) pairs in it.  By default, first value (t1) is the key and the second value (t2) is the value associated with the key.       Different ways to initialize a pair STL   1. declaring a pair and using make_pair( ) to make a pair :        pair < int, string > p ;     p = make_pair ( 10, "gamma" ) ;        2. using first and second keyword to make a pair:        pair < int, string > p ;     p.firs

All about stack STL in c++ for competitive programming( initialization, traversal and methods/functions ) :

stack STL Important Points: It is a type of container adaptor which follows LIFO (Last In First Out) rule. Elements are inserted and extracted from the top of the container only. It is same as the stack in c but with a huge advantage of availability of direct functions/methods for many operations. Remember to use the header file #include<stack> before using stack STL. How do i remember LIFO ? Assume a old version CD holder stand (a pillar on a circular base) in which we can put CDs. Now think of the CDs as elements and the CD holder as the container. A CD can only be put in the stand from the top and after a CD is put we can put the next CD on top of the previous one. To remove a particular CD we have to first remove the CDs on top of it which means LIFO (Last In First Out) i.e the CD which was put in last has to be removed out first.     stack STL initialization declaring a stack and using push( ) method to put elements into it from back:        stack < int > s ;     s