0% found this document useful (0 votes)
2 views

C++_Notes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++_Notes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

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)

You might also like