-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl.cpp
More file actions
475 lines (357 loc) · 12.9 KB
/
Copy pathstl.cpp
File metadata and controls
475 lines (357 loc) · 12.9 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <bits/stdc++.h>
using namespace std;
// ============================================================
// BASICS
// ============================================================
void print() {
cout << "print function";
}
int sum(int a, int b) {
return a + b;
}
// ============================================================
// STRING
// ============================================================
void explainString() {
string s = "hello";
s.push_back('!'); // "hello!"
s.pop_back(); // "hello"
s += " world"; // "hello world"
s.append(" world"); // same as +=
cout << s.length(); // or s.size()
cout << s[0]; // 'h'
cout << s.front(); // 'h'
cout << s.back(); // 'd'
// substr(start, length)
cout << s.substr(0, 5); // "hello"
// find — returns index, or string::npos if not found
int pos = s.find("world"); // 6
if (pos != string::npos) cout << "found at " << pos;
s.erase(5, 6); // erase 6 chars from index 5
s.insert(5, " C++"); // insert at index 5
sort(s.begin(), s.end()); // sort characters
// convert
int n = stoi("42"); // string → int
string t = to_string(42); // int → string
// begin, end, rbegin, rend, clear, empty, swap — same as vector
}
// ============================================================
// PAIR
// ============================================================
void explainPair() {
pair<int, int> p = {1, 5};
cout << p.first << " " << p.second; // 1 5
pair<int, pair<int, int>> p1 = {2, {4, 7}}; // nested pair
cout << p1.first << " " << p1.second.first << " " << p1.second.second; // 2 4 7
pair<int, int> arr[] = {{2, 3}, {4, 6}, {5, 8}};
cout << arr[1].second; // 6
}
// ============================================================
// VECTOR (dynamic array)
// ============================================================
void explainVector() {
vector<int> v;
v.push_back(1); // {1}
v.emplace_back(2); // {1,2} — faster than push_back
vector<pair<int, int>> vp;
vp.push_back({3, 7}); // must wrap in braces
vp.emplace_back(2, 5); // auto-constructs pair
vector<int> v2(5); // {0,0,0,0,0}
vector<int> v3(5, 13); // {13,13,13,13,13}
vector<int> v4(v3); // copy of v3
vector<int> v5 = {1, 4, 2, 5, 9};
// --- iterators ---
auto it = v5.begin(); // points to first element
it++;
cout << *it; // 4
it = it + 2;
cout << *it; // 5
// v5.end() → past-the-last (iterate with it--)
// v5.rbegin()→ last element (iterate with it++)
// v5.rend() → before-first (iterate with it--)
cout << v5.back(); // last element: 9
cout << v5[0]; // 1
cout << v5.at(1); // 4 (same as [], rarely used)
// --- loops ---
for (auto it = v5.begin(); it != v5.end(); it++)
cout << *it << " ";
for (auto x : v5) // range-for (preferred)
cout << x << " ";
// --- erase ---
v5.erase(v5.begin() + 1); // remove index 1
v5.erase(v5.begin() + 1, v5.begin() + 3); // remove [1, 3)
// --- insert ---
v5.insert(v5.begin(), 30); // {30, ...}
v5.insert(v5.begin() + 1, 2, 25); // insert 25 twice at index 1
v5.insert(v5.begin(), v4.begin(), v4.end()); // prepend v4
cout << v5.size();
v5.pop_back(); // remove last
v5.swap(v4); // swap contents
v5.clear();
cout << v5.empty(); // 1
}
// ============================================================
// LIST (doubly linked list)
// ============================================================
void explainList() {
list<int> ls;
ls.push_back(2); // {2}
ls.emplace_back(5); // {2,5}
ls.push_front(3); // {3,2,5}
ls.emplace_front(6);// {6,3,2,5}
// begin, end, rbegin, rend, clear, insert, size, swap — same as vector
}
// ============================================================
// DEQUE (double-ended queue)
// ============================================================
void explainDeque() {
deque<int> dq;
dq.push_back(2); // {2}
dq.emplace_back(5); // {2,5}
dq.push_front(3); // {3,2,5}
dq.emplace_front(6);// {6,3,2,5}
dq.pop_back(); // {6,3,2}
dq.pop_front(); // {3,2}
dq.back();
dq.front();
// begin, end, rbegin, rend, clear, insert, size, swap — same as vector
}
// ============================================================
// STACK (LIFO)
// ============================================================
void explainStack() {
stack<int> st;
st.push(1); // {1}
st.push(2); // {1,2}
st.push(3); // {1,2,3}
st.push(3); // {1,2,3,3}
st.emplace(5); // {1,2,3,3,5}
cout << st.top(); // 5 ← top is last-in
st.pop(); // {1,2,3,3}
cout << st.top(); // 3
cout << st.size(); // 4
cout << st.empty(); // 0
stack<int> st1, st2;
st1.swap(st2);
}
// ============================================================
// QUEUE (FIFO)
// ============================================================
void explainQueue() {
queue<int> q;
q.push(1); // {1}
q.push(2); // {1,2}
q.emplace(4); // {1,2,4}
q.back() += 5; // {1,2,9}
cout << q.back(); // 9
cout << q.front();// 1
q.pop(); // removes front → {2,9}
cout << q.front();// 2
// size, swap, empty — same as stack
}
// ============================================================
// PRIORITY QUEUE (heap)
// ============================================================
void explainPriorityQueue() {
// --- max heap (default) ---
priority_queue<int> maxpq;
maxpq.push(5); // {5}
maxpq.push(2); // {5,2}
maxpq.emplace(8); // {8,5,2}
cout << maxpq.top(); // 8
maxpq.pop();
cout << maxpq.top(); // 5
// --- min heap ---
priority_queue<int, vector<int>, greater<int>> minpq;
minpq.push(5); // {5}
minpq.push(2); // {2,5}
minpq.emplace(8); // {2,5,8}
cout << minpq.top(); // 2
// size, swap, empty — same as stack
}
// ============================================================
// SET (sorted + unique)
// ============================================================
void explainSet() {
set<int> st;
st.insert(1); // {1}
st.emplace(2); // {1,2}
st.insert(2); // {1,2} duplicate ignored
st.insert(4); // {1,2,4}
st.insert(3); // {1,2,3,4}
// begin, end, rbegin, rend, empty, size, swap — same as above
auto it = st.find(3); // iterator to 3
auto it2 = st.find(6);// points to end() — not found
st.erase(5); // no-op if not present
int cnt = st.count(4);// 0 or 1
auto it3 = st.find(2);
st.erase(it3); // O(1) with iterator
auto it4 = st.find(1);
auto it5 = st.find(4);
st.erase(it4, it5); // erase [it4, it5)
auto lb = st.lower_bound(2); // first element >= 2
auto ub = st.upper_bound(4); // first element > 4
// all operations O(log n)
}
// ============================================================
// MULTISET (sorted, allows duplicates)
// ============================================================
void explainMultiset() {
multiset<int> ms;
ms.insert(1); // {1}
ms.insert(1); // {1,1}
ms.insert(1); // {1,1,1}
ms.erase(1); // removes ALL 1s
int cnt = ms.count(1); // 3 (before erase)
ms.erase(ms.find(1)); // remove one 1
ms.erase(ms.find(1), next(ms.find(1), 2)); // remove two 1s
// other funcs same as set
}
// ============================================================
// UNORDERED SET (unique, no order)
// ============================================================
void explainUSet() {
unordered_set<int> st; // underscore required
// lower_bound / upper_bound NOT available
// rest same as set
// O(1) avg, O(n) worst case (rare hash collision)
}
// ============================================================
// MAP (sorted by key, unique keys)
// ============================================================
void explainMap() {
map<int, int> mp;
map<int, pair<int, int>> mp2;
map<pair<int, int>, int> mp3;
mp[1] = 2;
mp.emplace(3, 1);
mp.insert({2, 4});
mp3[{2, 3}] = 10;
for (auto& [key, val] : mp) // structured binding (C++17)
cout << key << " " << val;
cout << mp[1]; // 2
cout << mp[5]; // 0 — inserts default value if missing!
auto it = mp.find(3);
cout << (*it).second; // same as it->second (changed from original)
auto it2 = mp.find(5); // returns end() if not found
auto lb = mp.lower_bound(2);
auto ub = mp.upper_bound(3);
// erase, swap, size, empty — same as set
}
// ============================================================
// MULTIMAP (sorted, duplicate keys allowed)
// ============================================================
void explainMultimap() {
// same as map, but mp[key] is NOT available (ambiguous)
}
// ============================================================
// UNORDERED MAP (unique keys, no order)
// ============================================================
void explainUnorderedMap() {
// same tradeoff as unordered_set vs set: O(1) avg, O(n) worst
}
// ============================================================
// ALGORITHMS
// ============================================================
bool cmpBySecondDescFirst(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) return a.second < b.second; // sort by second asc
return a.first > b.first; // tie-break: first desc
}
void explainAlgo() {
int a[] = {3, 1, 4, 1, 5};
int n = 5;
sort(a, a + n); // array sort
sort(a + 2, a + 4); // sort subrange [2, 4)
sort(a, a + n, greater<int>()); // descending ← note: ()
vector<int> v = {3, 1, 4};
sort(v.begin(), v.end());
pair<int, int> pa[] = {{2, 1}, {1, 2}, {4, 1}};
sort(pa, pa + 3, cmpBySecondDescFirst);
// --- bit tricks ---
int num = 7; // binary: 111
int cnt = __builtin_popcount(num); // 3 — count set bits (int)
long long big = 23942931226161LL;
int cnt2 = __builtin_popcountll(big); // count set bits (long long)
// --- permutations ---
string s = "123";
sort(s.begin(), s.end()); // must start sorted for all permutations
do {
cout << s << " ";
} while (next_permutation(s.begin(), s.end())); // 123 132 213 231 312 321
// --- other ---
int mx = *max_element(a, a + n); // max value in array
int mn = *min_element(a, a + n); // min value in array
bool sorted = is_sorted(a, a + n);
// --- INT limits (image: "Tools: INT_MAX/MIN") ---
cout << INT_MAX; // 2147483647
cout << INT_MIN; // -2147483648
cout << LLONG_MAX; // long long max
}
// ============================================================
// COMMON PROBLEMS
// ============================================================
void problems() {
// ---- 1. Reverse a string ----
string s = "Hello";
// Option A: in-place
reverse(s.begin(), s.end());
// Option B: new string (preserves original)
string rev(s.rbegin(), s.rend());
// Option C: two-pointer manual
int l = 0, r = s.size() - 1;
while (l < r) {
swap(s[l++], s[r--]);
}
// ---- 2. Max / Min in Array ----
int a[] = {3, 1, 4, 1, 5, 9};
int n = 6;
cout << *max_element(a, a + n); // 9
cout << *min_element(a, a + n); // 1
// ---- 3. Char Frequencies ----
string str = "hello world";
map<char, int> freq;
for (char c : str)
freq[c]++;
for (auto& [ch, cnt] : freq)
cout << ch << ": " << cnt << "\n";
// unordered_map<char,int> for O(1) avg if order doesn't matter
// ---- 4. Check if Sorted ----
cout << is_sorted(a, a + n); // 0 (false)
// ---- 5. FizzBuzz ----
for (int i = 1; i <= 20; i++) {
if (i % 15 == 0) cout << "FizzBuzz\n";
else if (i % 3 == 0) cout << "Fizz\n";
else if (i % 5 == 0) cout << "Buzz\n";
else cout << i << "\n";
}
}
// ============================================================
// MAIN
// ============================================================
int main() {
ios_base::sync_with_stdio(false); // faster I/O — do NOT mix with scanf/printf
cin.tie(NULL); // don't flush cout before every cin
// use "\n" instead of endl (endl flushes buffer — slow)
print();
cout << sum(2, 9);
// containers
explainString();
explainPair();
explainVector();
explainList();
explainDeque();
explainStack();
explainQueue();
explainPriorityQueue();
explainSet();
explainMultiset();
explainUSet();
explainMap();
explainMultimap();
explainUnorderedMap();
// algorithms
explainAlgo();
// problems
problems();
return 0;
}