-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcppNote.txt
123 lines (106 loc) · 5.42 KB
/
cppNote.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
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
lValue vs rValue
https://www.youtube.com/watch?v=fbYknr-HPYE&ab_channel=TheCherno
rValue => something that is "temporary", something that is not able to be assigned a value
lValue => something that has a location in Memory. it is possible to get value assigned.
int getValue() => returns rValue => so getValue() = 5 (X)
int& getValue() => returns lValue => getValue() = 5 (O)
lValue reference : setValue(string& x);
rValue reference : setValue(string&& x);
Move
https://www.youtube.com/watch?v=ehMg6zvXuMY&ab_channel=TheCherno
https://en.cppreference.com/w/cpp/utility/move
std::move() => make something rvalue(T&&)[temporary]
So it just becomes a temporary object like 10 7 "asdas"...
If that value is moved, it becomes empty...
string x = "hello";
string y = move(x);
=> x is empty, y is "hello", when "hello" is moved from x, x's pointer is set to nullptr!
and the actual data that pointer refers to is moved to y's pointer
if we do not use move, then x = "hello", y = "hello"
CPP NOTES
Class design
(copy constructor, destrurtor, = operator overloading)!!
with default constructor + (move constructor and move = operator overloading)
VECTOR
.erase(pos) => erase the element at "pos", .erase(end()) is undefined! behavior
.erase(from, last) => erase [from , last), O(last-from)
from and last should be defined!
RETURNS :
Iterator "following" the last removed element!
If pos refers to the last element, then the end() iterator is returned.
If last==end() prior to removal, then the updated end() iterator is returned.
If [first, last) is an empty range, then last is returned.
.back() returns reference. So we can do v.back() = 7 (it will change!)
.insert(pos, val) or
.insert(pos, iter.begin, iter.end)
Insert elements right before the pos
Priority Queue<item, container, compare>
Compare class should overload () operator
bool operator(){const T& a, const T& b}{
return // ex) a < b;
}
For priority queue, our heap is contained in an Array.
It makes heap by Compare(parent, cur) == true : swap(par, cur)
So, if Compare returns parent < cur (usually ascending oreder),
then it makes maxHeap by making biggest elem to the root!
Then when it pop, swap(data[0], data.back()), then fixDown(0).
Thus, the order is reversed from when we are using a sort().
Function Type in CPP == similar to function pointer but more readable
function<R (...args)>
=> function<Return Type (params)> No comma between R and (...args)!
Example)
unordered_map<string, function<int (int, int) > > map = {
{ "+" , [] (int a, int b) { return a + b; } },
{ "-" , [] (int a, int b) { return a - b; } },
{ "*" , [] (int a, int b) { return a * b; } },
{ "/" , [] (int a, int b) { return a / b; } }
};
Function Pointers
int (*fcnPtr)();
fcnPtr is a function pointer that Takes no argument and retuns int.
We can do fcnPtr = &foo; foo is a function name
Optional
optional<T> someValue;
if(somaValue.has_value()) ...
This is used to differentiate empty(no value) with other values.
It is good for initializing or giving a default!
CPP vector erase() and clear()
Time complexity is O(N)!!
pop_back() is O(1)
lower_bound(begin, end, val , [comparator for "<"]) ** SHOULD BE AN INCREASING ORDER(use Rbegin for reverse order)
- comp(elem, val) = false 인 첫번째 아이템 **(elem >= val 인 첫 아이템)**
(elem < val)
ex) low = lower_bound (v.begin(), v.end(), 20,
[](const int& elem, const int& val){
return elem <= val;
});
This makes lower_bound like an upper_bound
(will return the first elem such that !(elem <= val), which means elem > val )
upper_bound(begin, end, val , [comparator for "<"])
- comp(val, elem) = true 인 첫번쨰 아이템 (elem > val 인 첫 아이템)
(val < elem)
ex) auto it = upper_bound(mp[key].begin(), mp[key].end(), timestamp,
[](const int &val, const pair<int, string > &a){
return val < a.first;
});
Getting the last element such that a < b => use Upper bound, and decrement the index!!
* Upper_bound gets the first element such that val < elem
* lower_bound gets the first element such that val <= elem
std::list(Doubly Linked List)
https://cplusplus.com/reference/list/list/?kw=list
push to front and back, popping from front and back are constants.
If we now the position of element to erase,
list.erase(pos) => constant!
This is the key difference from vector and deque
=> vector and deque should shift elements, so erase by position takes Linear
Cannot use std::sort(begin, end, Compare), use std::list.sort(Compare)
Be careful with type conversion! (int, size_t ,double) => division and subtraction!
int x = 1, y = 2;
(x + y) /2 => returns int because int is divided (1)
(double)(x + y) /2 => returns Double because double is divided(1.5)
(double)((small.top() + big.top()) / 2) => returns double but incorrect (1) becaue int is divided,
so the result is 1 then 1 is converted to double, so it returns 1.
Vector Equality (== operator)
if content is same? then true, else false
iter_swap(fiter, siter);
swap two item. same as swap(arr[i], arr[j])