/
NotesC++
142 lines (92 loc) · 3.73 KB
/
NotesC++
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Random useful syntax:
1. Initializing a vector:
vector<int> _data;
_data({10})
Also vector<int>idx(4, 100) // 4 elements initialized to 100.
2. Using a templatized node with shared_ptr.
shared_ptr<Node<T>>
3. Numeric limits of basic arithmetic data types
std::numeric_limits<unsigned>::max()
std::numeric_limits<unsigned>::min()
4. log10() Gives you number of digits in a decimal number
e.g. log (100000) = 5, number of digits = 5 + 1 = 6;
5. throw length_error();
throw invalid_argument();
6. Use stack.emplace to push on to stack without basic data types.'
7. Calling a templatized function.
If tempalte arugments can be deduced then don't need to specify data type
calculate<int>(5) is same as calculate(5);
however if you intend to have int as double then you should specify it
calculate<double>(5)
In EPI, some times a function is called like this
function_call<T>(arg1, arg2)
8. shared_ptr
in order to make a shared_ptr you should use
make_shared<ObjectType> T;
e.g. vector<shared_ptr<Node<T>> abc;
abc.push_back(make_shared<Node<T>>)
Other function is shared_ptr<int> sp1;
sp1.reset(new int(50));
9. cbegin() cend() when the iterator is const;
10. crbegin() crend() when you want const iterator but reverse
11. unordered set stores only unique elements.
so every element has "second " field which is a boolean which is set to true first time you insert the element.
it becomes false when next time someone else tries to insert the same element again.
very cool feature to figure out if the element exists already.
unordered_set<int> t
t.emplace(3).second == false;
12. make_heap(v.begin(), v.end());
pop_heap();
push_heap();
sort_heap();
13. vector.end() points to an index after the last element
hence while using
v.emplace_back(a.begin(), a.end()); works fine, a is another vector
but
v.emplace_back(a.begin() + idx, a.begin() + i); here i = the index you are trying to achieve +1.
14. istringstream for streaming incoming sequence
istringstream sin
T x; // x can be of anytype. if the conversion fails you could use if (sin.fail()) check
while(sin >> x)
{
}
15. unoredered_set can also take a tuple instead of single element.
unordered_Set<tuple<int,int,int>, HashTuple> cache
in this case you need to implement Hashtuple
16. iota(v.begin(), v.end, 0);
v = 0 ,1, 2, 3, ,4, 5, ,,,, till v.end()
17, string::substr
// syntax s.substr(3, 5) where 3 = starting index. 5 = number of letters after index 3 inclusive
Initializer Lists:
==================
1. Why should you using initializer list as opposed to normal initialization.
answer:
a) const data members: Must be initialized using initializer list
b) reference data members: int &a. Must be initialized using initializer list
c) initializing base class data members
if a Class A doesnt have a default constructor and it is a data member for class B, then class A must be initialized using initializer list in class B's constructor
class A
{
A(int x){
}
}
class B : A
{
A a;
B(int x): a(x){
}
}
d) when constructor's parameter name is same as data member
e) Performance reasons
Trees :
======
1. A Full binary tree: All nodes except non leaf nodes have two children
Also all non leaf nodes in a full binary tree are one less than the leaf nodes.
(can be proved by induction)
2. Similarly a perfect binary tree contains a total of 2^(h+1) - 1 nodes.
h being the height. out of which 2^h are leaf nodes.
When traversing a tree recursively, time complexity is O(n)
But space complexity is O(h) because at any point maximum nodes stored on the system stack would be <= h.
If we have parent field then we only need O(1) space.
3. A tree is considered balanced only if the difference in the height between left and right subtrees at a particular node n is at most one.
-