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 eqauting 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<>>
limitation => for continuous memory allocation =>for local declared array/vector maximum size possible 10^5 and for globally declared array/vector maximum size possible 10^7
Iterators - similar to pointers
- v.begin() => points to first element & v.end() => points after last element
- rbegin() => returns reverse iterator pointing to the last element in the container rend() => returns reverse iterator pointing to the theoretical element right before the first element in the set container
- 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 maps , 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
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++ and prev(it) same as it--
- 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 that 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
Post a Comment