C++_Notes
C++_Notes
C++ STL:
Pair:
If we use ref variable and change the values then p.first value
Vector dynamic size. Size depends on number of elements we added to it.
vector<int> v; v.size() =0
v.push_back(1); v.size() =1
Copying one vector to another takes O(n) time complexity. So it’s better to pass reference of that vector
as arguments to functions instead of copies.
We can either use make_pair function or use brackets directly {}
Array of vectors:
In Vectors it++ and it+1 gives the same output since vector has continuous memory allocation but in
maps and sets it+1 points to next location which may not necessarily where the next element is present.
and it++ points to the next iterator where the next element is present.
we can also use
We can use two kinds of loops for vectors. Below value is copy of the vector elements not reference to
values in the vector . If we need reference we can use & operator
In this case values in the vector gets
incremented
auto keyword
Maps: Implemented using Red Black trees Keys are stored in sorted order
The insertion operation in map takes O(nlogn) time
Even though we did not assign value to m[6] just by declaring it inserts null to m[6]. m.find() operation also take O(nlogn) time.
Because of Internal Hash table implementation Insertion and access has O(1) complexity
m.erase()O(1)
Not all data types can be used a keys in unordered map unlike map because of its internal Hash table
implementation . Like Keys cannot be pair, vector, set etc..
Set: (No duplicated and sorted order implemented using red black trees)
If value is removed from map then we get the set. Both Loops produces same output.
Multiset:
Duplicates are allowed
Reversing a string
std::string is a class in the C++ Standard Library that represents a sequence of characters and provides numerous member
functions for string manipulation.
Memory Management:
Size:
https://github.com/loveBabbar/CodeHelp-DSA-Busted-Series/blob/main/Lecture022%20Strings/validPalindrome.cpp
strcmp(s1,s2) 0 if equal
strcpy(dest,src)