4. STL

                                                   STL                       

A utility library having algorithms, containers, functions, iterator

------------------------------------------------------------------------------------------------------------------------

  • pair, vectors list, deque        ✅
  • set, map, priority queue         ✅
  • stack , queue                          ✅

-----------------------------------------------------------------------------------------------------------------------

                                                   PAIRS

pair <int, int> p = {1,3}

cout << p.first ;


we can store multiple data by nesting with pairs

e.g., pair <int,int> arr[] =[ {1,2}, {3,4} , {5,6} ]

cout << arr[1].second;

--------------------------------------------------------------------------------------------------------------------------

                                       VECTORS (container)

It is a dynamic array.
Here, we can modify size even after initialising its size.

vector <int> v;

--------------------------------------------------

Insertion-1
v.push_back(1);                  
v.emplace_back(2);    

size nhi diya toh pushback, but agar dediya toh v[i] se, insert krenge

vector < pair <int,int>>  v;
v.push_back({x,y});
v.emplace_back(x,y); 
-------------------------------------

                                                                        Initialising-

vector <int> v (5)  ----> container of size 5 with all zeroes
vector <int> v (5,100) --> all 5 are 100
--------------------------------------------

                                                                              Copy-

vector <int> v2(v1);
there are continuous memory locations in a vector.
-------------------------------------------

                                                                Creating Iterators on a vector-

1. vector<int> ::iterator x: v.begin()
or,
2. auto x: v.begin()
or,
3. auto x: v

----------------------------------------------------
v.end()    -->   points to posn just after the last element
v.rend() -->    iterator which points to the left of 1st elmt
v.rbegin() --> iterator on last element

v.back() -->    gives last element
-------------------------------------------

                                                                             Looping

Method-1: 

for(vector<int>::iterator x:v.begin(); x!=v.end(); x++){
            cout<<*(x);
}
---------------------
Method-2: 





-----------------------------------------------
                                                                              Deletion-

v.erase(a,b)- removes from [a,b) posn

v.erase(v.begin());                           // removes first element

v.erase(v.begin()+1,v.begin()+4);  // end is not included like [a,b)

-------------------------------------------------

                                                                           Insertion-2

v.insert(pos, occurrences, elmt)




------------------
vector<int> v = {1, 2, 5};
v.insert(v.begin() + 2, {3, 4});  --> 3 & 4 inserted at posn 2  
                                                // v becomes: {1, 2, 3, 4, 5}
------------------
vector<int> v2 = {10, 20, 30};
v.insert(v.end(), v2.begin(), v2.end());  
                                                // Merges v2 into v at the end
----------------------------------------------------------------------------

                                                                        Vector Functions

cout<<v.size();

v.pop_back();      pops out last element in O(1)

v1.swap(v2);       swaps 2 vectors in O(1)

v.clear();             empties the vector in O(N)  

v.empty()           checks if empty or not

-----------------------------------------------------------------------------------------------------------------------------
                                                                               LIST

Add elements to the front as well as the back in constant time
At its core, it is just an implementation of a doubly linked list.

list <int> ls;
ls.push_front(3);     {3,1,2}
ls.emplace_front(4);   {4,3,1,2}

-----------------------------------------------------------------------------------------------------------------------------

                                                                            DEQUE

deque<int>dq;

1. It is an implementation of a segmented dynamic array.
2. Here also, we can insert/ delete elements at front and back in constant time, just like a list
3. Random access is also allowed in O(1) 

---------------------------------------------------------------------------------------------------------------------

vector- (add/del at end + random access) => constant time

list-      (add/del at start/end) => constant time

deque- (add/del at start/end + random access)=> constant time

------------------------------------------------------------------------------------------------------------------------

                                                                             Stack

                                                                              L I F O                                                                             

stack<int>st;

push inserts element at the end (top)
pop deletes element at the end

All operations in stack are in O(1)

---------------------------------------------------------------------------------------------------------------------------

                                                                        QUEUE 

F I F O

front of queue-  first element in container
back of queue-  last element in container 

push inserts element at the end
pop deletes element at the start (front)

----------------------------------------------------------------------------------------------------------------------------

                                                             PRIORITY - QUEUE

by default, it is a max heap (elements stored in Descending order)

non linear data structure

priority_queue <int> pq;

------------------------------------------------------------------------------------------------------------------------

                                                                              SET-
non linear container
everything is sorted and unique.
set<int>s;
---------------------------------------------------------

st.insert(x)
st.count(x)                  // checks if 'x' exists or not
st.erase(x)

auto it = st.find(x);
    // returns pointer to element whose value is x
st.erase( it1, it2 ) 
     // erases all elements from [it1,it2)

erase(x) takes log time   VS   erase(it) takes const time
------------------------------------------------------------------

                                                                         Multiset-

sorted rule + non unique

multiset<int>ms;






two 1's erased, e.g., {1,1,1,2,3,8}
rest functions same as set
---------------------------------------------------------------------
                                                             Unordered Set-
unique but not sorted
has better complexity than set in most cases, except for collision.
---------------------------------------------------------------------------------------------------------------------------
                                                                             MAP-

1. keys are unique
, values can be duplicate.
2. key and value can be any data type.
3. it stores unique keys in sorted order
-------------------------
e.g., 
map<int, int> mpp;
mpp[1]=2;    
mpp.insert({5,2});    // mpp[5]=2;
mpp.emplace({7,6})
--------------------------
iteration-
--------------------------------------------------------------------
Finding value corresponding to a key:-

.find(x) returns iterator to (key=x, value) pair


   
------------------------------------------------------------------
                                                Multimap & Unordered Map-
----------------------------------------------------------------------------------------------------------------------
NOTE-

For sorted containers, we can use lower and upper bound concept-
✔ st.lower_bound(x) → If you want ≥ x (useful for range queries)
                                    prints first elmt which is not less than x
✔ st.upper_bound(x) → If you want > x (useful for nge)
                                     prints first elmt greater than x
------------------------------------------------------------------


-----------------------------------------------------------------------------------------------------------------------
                                                                    SORTING-
1. Ascending order:-

2. Descending order:-

3. Custom order:- 
                 
e.g.,
comp is a custom comparator function










---------------------------------------------------------------------------------------------------------------------------

                                                                     MAX-MIN

a is any container

-----------------------------------------------------------------------------------------------------------------------------

                                                               Built-in PopCount

returns no. of set bits

int num = 21124;

int y = __builtin_popcountll(num);     // for 64-bit numbers

for 32-bit int remove ll;

---------------------------------------------------------------------------------------------------------------------------

                                                               Next Permutation

                                                                           (v. important)

to get all possible permutations of a string as per dictionary, starting from the given permutation.

e.g., 231 ke liye 1 se start hone wale nhi aayenge balki 2 se aayenge

so to get all permutations first sort it out.

-------------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

30.) DP on subsequences

19. Strings (LB)

32. Binary Trees-2 (bfs/dfs med problems)