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;
--------------------------------------------------
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};
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
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)
Multiset-
sorted rule + non unique
multiset<int>ms;
Unordered Set-
1. keys are unique, values can be duplicate.
map<int, int> mpp;
NOTE-
lower_bound(x) → If you want ≥ x (useful for range queries)✔ st.
upper_bound(x) → If you want > x (useful for nge)-----------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
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
Post a Comment