-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_notes.txt
78 lines (50 loc) · 2.56 KB
/
cpp_notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Here are all the cpp notes that I have written and understood!
=====================================
in CPP, sorting something a vector of pair by its second element, you can use the inbuilt function in c++
it goes like this:
---
sort(v.begin(), v.end(), sort_by_sec);
---
Driver fucntion fot it
---
bool sort_by_sec(cont std::pair &a, const std::pair &b){
return a.second < b.second;
}
---
This method exploits the fact that, in any sorting method, comparisions are made. Ultimately, this code allows the inbuilt algorithm to compare directly the second element of the pair
============================================
declaring a class object in C++
classname obj(parameters);
=============================================
Disjoint Set Note!:
Once you find parents make sure to union the sets. I.e. rreset their parents!
============================================================================
in c++
bits/stdc++.h has strings in it.
string.length() : length of the string
string.at(index) : char at an index
string.substr(start, end) : gets substring between start and end index
About maps:
maps initialize all values to 0. This is quite frustrating in dynamic programming when generating the
table. Coz all keys of the map are initialized to 0. Hence, if a fucntions return 0 as a value the function still things that the value isn't computed and hence can cause an infinite loop. instead initialize actual 0 values to -1. Then the next best thign you can do is, use "return (table[val] == -1?0:table[val]) "
============================================================================
Ouuu... While doing merge sort I found a lot of usefull things
vectors; merging vectors in c ++
assume you have
std::vector<int> AB, A = {1,2,3,4},B = {5,6,7,8};
then do:
AB.reserve(A.size()+B.size());
AB.insert(AB.end(), A.begin().A.end());
AB.insert(AB.end(), B.begin(), B.end());
this will give us AB = {1,2,3,4,5,6,7,8}
-----
Now, to check if a vector is empty: vector.empty() (boolean value return)
to erase an element (or range of elements) in the vector: vector.erase(vector.begin() + index, (optional) vector.begin()+end)
=============================================================================
passing arrays and initilazing values in array:
pass 2d array just by name.
declare function as:type func(type array_name[][size]);
initialize memory:
memset(arrayname, value_to_be_initialized, sizeof(arrayname[0][0]...[0])*dim1*dim2*...dimr);
^ initializes a n dimentional array by value_to_be_initialized
============================================================================