We should pass the size along with array to a function because when we are trying to find the size
of the array using sizeof we will
get total size of the array whether whole array is filled or not.
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.
m.erase(3) removes element with key =3
m.clear(); clears map
insertion operation also depends on size of the key
if string size is too long
Unordered Map(Implemented using Hash table): Keys are not in sorted order
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...
Multimap allows duplicate keys.
Try to use Unordered maps if possible, when sorting is not required since takes less time.
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.
To delete an element, we use s.erase(it);
Unordered Set: (uses hash functions in internal implementation)
Multiset:
Duplicates are allowed
deletes all occurrences.
Strings and Character Arrays:
Last Element in character array is null ‘\0’ same as in String
Anywhere null pointer is present it won’t be processed further
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:
Character Array: Requires manual memory management if dynamically allocated.
std::string: Handles memory management automatically.
Size:
Character Array: Fixed size; cannot be resized after declaration.
std::string: Dynamic size; can be resized as needed.
https://github.com/loveBabbar/CodeHelp-DSA-Busted-Series/blob/main/Lecture022%20Strings/validPalindrome.cpp
cin Execution stops when “ “ or “\t” or “\n” is triggered
we can use cin.getline(pointer to char array ,len)
By default, getline uses the newline character ('\n') as the delimiter, but you can specify any character as the delimiter.
cin.getline(str, SIZE, ';'); reads input into str up to a semicolon (;).
strcmp(s1,s2) 0 if equal
strcpy(dest,src)
CORE JAVA
Generics Watch Coding with John video
Primitive datatypes are stored in STACK itself. Objects are stored in heap
Everything that starts with a capital letter is called Class. String s=”Pavani”;
String s1=”Pavani”;
s==s1 gives true.
s1=”Reddy”; works but s1 is now pointing to Reddy object not “Pavani”; we did not change Pavani to Reddy here
Strings are immutable because in string pool two or more ref variables can point to the same object and if one of them
changes the object the value would be changed. This can be against security so they made strings immutable in java.
String pool: Separate Memory Structure inside heap so that similar strings do not have to be recreated again.
When you only want to compare values then we can use .equals method .
a.equals(b)true
charAt(index)gives character.
System.out.println(56)o/p:56
System.out.println(“pavani”)o/p:pavani
If obj ==NULL it just
prints NULL
Now if we want to print an object for e.g an array
this is
happening because of below toString function
So we use
we are telling java to use toString method in arrays . Now o/p would be [2,3,4,5] we did
function overriding here
O/P: 56
56 even though we did not use
num.toString just using num also it prints 56 because it calls valueOf function which futher
calls toString
we should use printf
here not println
printf rounds off the value
Pretty Printing uses printf
o/p:- 195
ab
o/p- :d
o/p:100
o/p:- Kunal[]
o/p:- Kunal56
erro
r because + operator only works for primitive datatypes and if at least one of them is a
string
In Java operator overloading is not supported unlike in C++ and python.
Since Strings are immutable in java if we change the value then the ref variable points to
the new object. We can use String Builder to avoid creation of new object and can make
changes to old object.
StringBuilder
No new object will be created every time we
add a new character.
Note that name.toLowerCase() does not modify the original object.
o/p:-[Kunal, Kushwaha]