-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cpp
More file actions
109 lines (90 loc) · 2.03 KB
/
Copy pathio.cpp
File metadata and controls
109 lines (90 loc) · 2.03 KB
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
/*
Get Formatted date
*/
string date = "2022-10-17";
int year, day, month;
sscanf(date.c_str(), "%d-%d-%d", &year, &month, &day);
int d[100010];
// memset to -1
memset(d, 0xff, sizeof d);
// memset to 0x3f3f3f3f
memset(d, 0x3f, sizeof d);
/*
Self defined sort methods
1. override < inside struct
2. write a cmp function
*/
// 1
struct Edges {
int a, b, w;
bool operator<(const Edges& e) const {
return w < e.w;
}
} edges[m];
sort(edges, edges + m);
// 2
struct Edges {
int a, b, w;
} edges[m];
bool cmp(const Edges& e1, const Edges& e2) {
return e1.w < e2.w;
}
sort(edges, edges + m, cmp);
/*
Sum all values in string or vector or array
*/
int getSum(vector<int>& arr) {
return reduce(arr.begin(), arr.end());
}
/*
HashMap setting empty value
*/
void work(int item) {
unordered_map<int, int> mp;
mp[item]; // no need to set it to 0
}
/*
Deconstructing array or pair
*/
pair<int, int> cur = {1, 2};
auto [x, y] = cur;
// x is now 1, y is now 2
// no need of cur.first and cur.second
array<int, 3> arr = {1, 0, -1};
auto [a, b, c] = arr;
// a is now 1, b is now 0, c is now -1
/*
Debug
*/
#define deb(x) cout << #x << " " << x
int ten = 10;
deb(ten); // prints "ten = 10"
/*
Even powerful debug marco
*/
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values << endl, delim = ", "));
}
int xx = 3, yy = 10, xxyy = 103;
deb(xx); // prints "xx = 3"
deb(xx, yy, xxyy); // prints "xx, yy, xxyy = 3, 10, 103"
/*
More Powerful Debug Macro with input and output
*/
template<typename F>
auto debug_func(const F& func) {
return [func](auto &&...args) { // forward reference
cout << "input = ";
printer(args...);
auto res = func(forward<decltype(args)>(args)...);
cout << "res = " << res << endl;
return res;
};
}
debug_func(pow)(2, 3);
// ^ this automatically prints
// input = 2 3 res = 8