Basics

Comment - for In-line comment (means comment in same line) --> Use // 
                   
for multi-line comment --> Use /* ........ */

Array - int arr[10] = {4, 5, 8} --> creates array of size 10 with first 3 elements known 

  • Access - 10[arr] or arr[10]  --> both correct bcz *(10+arr) and *(arr+10) are same

Pair - pair<int,string> p = {2 , "fsf"}

  • How to access ?
    • p.first and p.second
Vector - array with dynamic memory size => continuous memory allocation

  • How to initialize vector ?
    • vector <double> v(10) => declares vector of size 10 with each value as 0
    • vector <double> v(7 , 3) => declares vector of size 7 with each value as 3
    • vector <double> v = {1, 3, 5, 10} => declares vector of with given values
    • vector <double> v(st.begin(), st.end()) => declares vector from given set

  • vector <int> v1 =v 
    • this copies the vector v1 as v (NOT possible DIRECTLY in array)
    • Although it may seem to be equating but it is of O(n) time 

  • v.resize(8, -3); ---> resized to size = 8 with default values as -3
    • new size = current size --> nothing happens
      new size < current size ---> extra elements are deleted
      new size > current size ---> extra spaces initialized with zero or given value
    • vv.resize(10, vi(4)) ---> resized for vector<vector<>>

  • v.assign(7, 10) - create 10 (7 times)            v.assign(s.begin(), s.end()) - given set as I/P
    • assign new values to given vector by replacing old ones
    • v.assign() is same as {v.erase() + v.resize()} → But assign is more optimized

  • v.clear() - remove all elements from vector
    • v.erase(v.begin() + 2) - removes element at 2nd index
    • v.erase(v.begin() + 1, v.begin() + 4) - Remove elements in range [1, 4)
      • erase returns iterator to index after last removed element/vector.end()

  • deque and list are similar to vector → but can do operations in front as well
    • dq.push_front(), dq.front(), dq.pop_front()

limitation for array/vector → for locally declared → max size is 10^5 and for global is 10^7 

Iterators - similar to pointers 
  • v.begin() => points to first element & v.end() => points after last element 
  • rbegin() => reverse iterator pointing to the last element in the container
    rend() => reverse iterator pointing to theoretical element right before first element
    • Used for reverse printing

  • How to initialize?
    • vector<pair<int,int>> :: iterator it = v.begin()
  • How to access ?
    • *it => just like pointer
    • vector<pair<int,int>>  ---> *it.first or it→first  {BOTH ARE SAME}
  • it++    =>  it moves to next iterator
  • it+1    =>  it moves to next location, i.e., current location +1
  • vector mai toh memory continuous hota hai toh it++ and it+1 are same but map/set mai different hota hai => ALWAYS  USE  it++
Range Based loops
  • Directly iterate on values of container {NO  need to use index}
    • for (int val : v) {cout<< val}
    • NOTE - here values in val get copied => [NO  CHANGE in original vector]
  • for direct access use reference variable
    • for (pair<int,int>  &val : v) {cout<< val.first}
Auto keyword - automatically takes the data type of container
  • vector<pair<int,int>> :: iterator it = v.begin()         is same as        auto it = v.begin() 
  • auto → recommended only with iterators bcz in others it may not dereference the type
    • auto arr = {1, 2} → Is it pair<int, int>   OR   vector<int>   OR   tuple<int, int>
      • Hard to identify here → use auto where it can easily dereference
    • But in iterators → like in above, it can dereference from type of v
Tuple - can hold multiple elements of different data types
  • tuple<int, char, char> yu = {1, '1', '2'}; ✅            auto yu = make_tuple(1, '1', '2'); ✅
    • make_tuple() must be used with auto (else it can't dereference the type)
  • To access 0th index → get<0>(yu)       OR     auto [a, b] = yu → extract all values of yu
  • In this case → get<0>(yu) ✅   get<int>(yu) ✅ → Both correct bcz only 1 int type
    • get<char>(yu) → ERROR bcz 2 char present at 1st index and 2nd index
  • To swap two tuples → tuple_1.swap(tuple_2)
  

Ordered map - generate key in sorted order (if strings => lexographical order)

  • You cannot update value of key => m[6]=3 in map -> can't update 6 
  • next(it) same as it++  prev(it) same as it-- [NOTE next/prev don't modify original it]
    • next(it, n) → moves iterator forward by n steps (by default, n = 1)
    • advance(it, n) → same as next(it, n) but modifies iterator it (in-place)
    • NOTE prev(it, n) is exactly same as next(it, -n)

  • How it is declared ?
    • map<int , string> m
  • How to assign value to each elements
    • m[1] ="abc"  =>1 is key and abc is its value
  • How to print ?
    • for(it = m.begin() ; it !=m.end() ; it++) { cout<< *it.first <<" " <<*it.second }
  • auto it = m.find(3)  => returns iterator pointing to that key
  • m.erase(3) => erases key 3 from map
    • m.erase(it) => erases key where iterator is pointing in map
    • NOTE :-  for strings time takes O(string.size() * log(n))

Unordered map - generate key in random manner => works on Hashing
  • BENEFIT - Time complexity becomes O(1) in all above operations mentioned above
  • LIMITATION  -  Can't be used for vector , pairs bcz no in-built hash table defined  
    BUT ordered maps just compare values => can be used in ordered maps

Sets - maps without value wala part
  • How to assign value to each elements
    • s.insert("abc")
    • here m[]="abc"  => NOT defined here
  • How to check its presence?
    • if (s.find("abc") != s.end()) => it's present !! 

Multi sets - set with duplicate allowed
  • auto it = s.find("abc")  => returns first iterator pointing to that value
  • m.erase("abc") => erases all "abc" from set
    • m.erase(it) => erases only value of that particular iterator
  • if s.insert("abc") two times => in output "abc"  printed two times

Comments

Popular posts from this blog

Graphs

Policy based data structures (PBDS)

Hashing