C++ CoreJava Python Notes
C++ CoreJava Python 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.
auto keyword
Maps: Implemented using Red Black trees Keys are stored in sorted order
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
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.
To delete an element, we use s.erase(it);
Unordered Set: (uses hash functions in internal implementation)
Multiset:
Duplicates are allowed
deletes all occurrences.
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
cin Execution stops when “ “ or “\t” or “\n” is triggered
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
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
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
o/p:-[Kunal, Kushwaha]