Uploaded by shelly peng

COMP2012H Cheasheet

advertisement
Kee Meng Tan -=- 20900440 -=- COMP2012H -=- Good Luck! -=- Friday 9 December 2022 -=- Year 1 Fall
1 // arrays
2
// static 1D array:
3
int x[3] = { 0, 1, 2 };
4
int* xp = x; int* xp = &x;
5
6
// dynamic 1D array
7
int* x = new int[3];
x[i] = ++i instead of i++, x[i] is
8
for (int i = 0; i < 3; i++, x[i] = i);
ONLY
in
9
&x != &x[0]; // address of x is
not POSSIBLE
the array address!
DYNAMIC ARR!!!
10
delete [] x; // make sure to delete!
11
12
x == &x == &x[0]; // address of array equals the address of the first element
13
*x == x[0]; // dereferencing address of array equals to the value of the first element
14
&x[i] == x+i // arithmetic on pointer to get address
15
*(x+i) == x[i] // arithmetic on pointer to get value
16
17
// static 2D array:
18
int x[3][3] = {
19
{ 0, 1, 2 },
20
{ 3, 4, 5 },
21
{ 6, 7, 8 }
22
};
23
24
// dynamic 2D array:
25
int** x = new (int*)[3];
26
for (int i = 0; i < 3; i++) {
27
x[i] = new int[3];
28
// Dynamic array elements can be accessed like static array elements
29
for (int j = 0; j < 3; j++, x[i][j] = 3*i+j); // eg set values to above 3x3 matrix
30
}
31
32
x[i] == &x[i][0]; // x[i] is the address of the first element of the ith row
33
x[i][j] == *(x[i] + j) == *(&x[i][0] + j) == *((*(x+i)) + j) == *(x[0] + 4*i + j) == *(&x[0][0] +
4*i + j);
34
35
36 // class member functions
37
// special member functions
38
X::X(); // default constructor: X x(); X* x = new X();
39
X::~X(); // destructor: delete x;
40
X(const X&); // copy constructor: X x(other);
41
X& operator=(const X& other); // copy assignment: X x = other;
42
X(X&& other); //move constructor: X x(move(other));
43
X& operator=(X&& other); // move-assignment operator: X x = move(other);
44
// make sure to check if(this != &other)
45
46
// other member functions
47
X::X(T a); // conversion constructor: x = a;
48
// special member functions generation
49
// Automatically generates the default-constructor, destructor, copy constructor, copy-assignment
operator, move constructor and move-assignment operator
50
// If a move constructor or move-assignment operator is explicitly declared:
51
//
Copy constructor, copy-assignment operator is not generated
52
// If a copy constructor, copy-assignment operator, move constructor, move-assignment operator,
or destructor is explicitly declared:
53
//
Move constructor, move-assignment operator is not generated
54
= default // automatically generate
55
= delete // do not allow function to be called
56
57
// member functions keywords
58
virtual // allows inherited classes to call/override function, and makes all inherited functions
virtual too
59
override // overrides base class function
60
static // space for the static variable is allocated only once and the value of variable in the
previous call gets carried through the next function call, and it is not counted in sizeof(), has to be
initalised outside of the class
61
extern // specifies that a global variable, function, or template declaration has external
linkage (functions are global by default)
62
inline // replace calls of this function with a single instance of the function
63
constexpr // evaluate the value of the function or variable at compile time
64
friend // is not inherited, is not mutual, allows all private data members to be accessed
65
66
// others
67
friend ostream& operator<<(ostream& os, const X& a) { return os };
68
// When a derived-class object is destroyed, the destructors are called in the reverse order of
the constructors
69
// default can only be declared once, and is set at compile time
70
71 // Trees
72
// Pre-order: root, left subtree, right subtree (depth first search, copy tree)
73
// In-order: left subtree, root, right subtree (always small to large)
74
// Post-order: left subtree, right subtree, root (delete tree from leaf to root)
75
76
// avl (Adelson-Velsky and Landis) trees
77
template <typename T> void AVL<T>::balance() {
78
if (is_empty()) return;
79
fix_height();
80
int balance_factor = bfactor();
81
if (balance_factor == 2) { // Right subtree is taller by 2
82
if (right_subtree().bfactor() < 0) // Case 4: insertion to the L of RT
83
right_subtree().rotate_right();
84
return rotate_left(); // Cases 1 or 4: Insertion to the R/L of RT
85
} else if (balance_factor == -2) { // Left subtree is taller by 2
86
if (left_subtree().bfactor() > 0) // Case 3: insertion to the R of LT
87
left_subtree().rotate_left();
88
return rotate_right(); // Cases 2 or 3: insertion to the L/R of LT
89
}
90
}
91
// Balancing is not required for the remaining cases
92
// deleting
93
// The node to be removed is a leaf node
94
// Delete the leaf node immediately
95
// The node to be removed has 1 child
96
// Adjust a pointer to bypass the deleted node and then delete (set nullptr)
97
// The node to be removed has 2 children
98
// Replace the node to be removed with either the maximum node in its left sub-tree, or
minimum node in its right sub-tree Then remove the max/min node depending on the choice above.
99
100 // rvalue references
101
// An lvalue designates a function or an object.
102
// If E is an expression of pointer type, then *E is an lvalue expression referring to the object
or function to which E points. Or a function returning an lvalue reference is an lvalue.
103
// An xvalue (an “eXpiring” value) also refers to an object near the end of its lifetime
104
// A function returning an rvalue reference is an xvalue.
106
// A glvalue (“generalized” lvalue) is an lvalue or an xvalue.
107
// An rvalue is an xvalue, a temporary object that is not associated with an object.
108
// A prvalue (“pure” rvalue) is an rvalue that is not an xvalue.
109
// A function returning not a reference (literal) such as 1, or true is also a prvalue.
110
111 // pointers (read from right to left!)
112
int * // pointer to int
113
int const * || const int * // pointer to const int
114
int * const // const pointer to int
115
int const * const || const int * const // const pointer to const int
116
int ** const // const pointer to pointer to an int.
117
int * const * // pointer to const pointer to an int.
118
int const ** // pointer to a pointer to a const int.
119
int * const * const // const pointer to a const pointer to an int.
120
121 // hashing
122
// linear: hi(k) = (hash(k) + i) mod m
123
// quadratic: hi(k) = (hash(k) + i2) mod m
124
// double: hi(k) = (hash(k) + i × hash2(k)) mod m
125
// alpha = n elements / m buckets
126
127 // lambda
128
int a = 1, b = 1, c = 1;
129
auto f = [a, &b, &c]() mutable { // a=1, &b=2, &c=2
130
auto g = [a, b, &c]() mutable { // a=1, b=2, &c=3
131
cout << a << b << c << endl; // 123
132
a = b = c = 4; }; // c=4
133
a = b = c = 3; g(); }; // b=3
134
a = b = c = 2; f(); // a=2
135
cout << a << b << c << endl; // 234
Download