-
Notifications
You must be signed in to change notification settings - Fork 0
/
STL .cpp
425 lines (255 loc) · 7.37 KB
/
STL .cpp
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
#include <bits/stdc++.h>
using namespace std;
void explainPair()
{
// pair <int, int> p={1,3};
// cout << p.first << " " << p.second;
// pair <int,pair <int,int>> p={1,{3,4}};
// cout << p.first << " " << p.second.second << " " << p.second.first;
// pair<int, int> arr[] = {{1,2},{3,4},{5,1}};
// cout << arr[2].first;
}
void explainVector()
{
vector<int> v; // dynamic memory
v.push_back(1);
v.emplace_back(2);
vector<pair<int, int>> vec;
v.push_back({1, 2});
v.emplace_back(1, 2);
vector<int> v(5, 100); //{500, 500, 500, 500, 500}
vector<int> v(5); //{0, 0, 0, 0, 0};
vector<int> v1(5, 20); // {20 ,20, 20, 20, 20}
vector<int> v2(v1);
vector<int>::iterator it = v.begin();
it++;
cout << *(it) << " ";
vector<int>::iterator it = v.end();
vector<int>::iterator it = v.rend();
vector<int>::iterator it = v.rbegin();
cout << v[0] << " " << v.at(0);
cout << v.back() << " ";
for (vector<int>::iterator it = v.begin(); it != v.end(); i++)
{
cout << *(it) << " ";
}
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *(it) << " ";
}
//{10, 20, 12, 23}
v.erase(v.begin() + 1);
//{10, 20, 12, 23, 35}
v.erase(v.begin()) + 2, v.begin() + 4; //{10, 20, 35}{start, end}
// Insert funcation
vector<int> v{2, 100};
v.insert(v.begin(), 300); // {300, 100, 100}
v.insert(v.begin() + 1, 2, 3, 10) // {300,10,10,100,100}
vector<int>
copy(2, 50); //{50, 50}
v.insert(v.begin(), copy.begin(), copy.end()); //{50,50,300,10,10,100,100}
//{10, 20}
cout << v.size(); // 2
//{10,20}
v.pop_back(); //{10}
// v1 -> {10,20};
// v1 -> {30,40};
v1.swap(v2); //{30, 40}, v2 -> {10, 20}
v.clear(); // erases the entire vector
cout << v.empty();
}
void explainList()
{
list<int> ls;
ls.push_back(2); //{2}
ls.emplace_back(4) //{2,4}
ls.push_front(5); // {5, 2, 4}
ls.emplace_front(); //{2,4}
// rest function same as vector
// begin, end, rbegin, rend, clear, insert, size, swap
}
void explainDeque()
{
deque<int> dq;
dq.push_back(1); // {1}
dq.empty_back(2); // {1, 2}
dq.push_from(4); // {4, 1, 2}
dq.emplace_front(3); // {3, 4, 1, 2}
dq.pop_back(); // {3, 4, 1}
dq.pop_front(); // {4 ,1}
dp.back();
dp.front();
// rest function same as vector
// begin, end, rbegin, rend, clear, insert, size, swap
}
void explainStack()
{ // Stack look like LIFO
stack<int> st;
st.push(1); //{1}
st.push(2); //{2,1}
st.push(3); //{3, 2, 1}
st.emplace(5); // {5, 3, 3, 2, 1}
cout << st.top(); // prints 5 "** st[2] is invalid **"
st.pop(); // st look like {3,3,2,1}
cout << st.top(); // 3
cout << st.size(); // 4
cout << st.empty();
stack<int> st1, st2;
st1.swap(st2);
}
void explainQueue()
{ // it's look like FIFO
queue<int> q;
q.push(1); //{1}
q.push(2); // {1, 2}
q.emplace(4); // {1, 2, 4}
q.back() += 5;
cout << q.back(); // prints 9
// Q is {1, 2, 9}
cout << q.front(); // prints 1
q.pop(); //{2, 9}
cout << q.front(); // prints 2
// size swap empty same as stack
}
void explainPQ()
{
priority_queue<int> pq;
pq.push(5); //{5};
pq.push(2); //{5, 2};
pq.push(8); //{8, 5, 2}
pq.emplace(10) //{10,8,5,2}
cout
<< pq.top(); // prints 10
pq.pop(); //{8,5,2}
cout << pq.top(); // prints 8
// size swap empty function same as other
// Minimum help
priority_queue<int, vector<int>, greater<int>> pq;
pq.push(5); //{5}
pq.push(2); //{2, 5}
pq.push(8); //{2, 5, 8}
pq.emplace(10); //{2, 5, 8, 10}
cout << pq.top(); // prints 2
}
void explainSet()
{
set<int> st;
st.insert(1); // {1}
st.emplace(2) //{1,2}
st.insert(2); //{1,2}
st.insert(4); //{1,2,4}
st.insert(3); //{1,2,3,4}
// Functionality of insert in vector
// can be used also, that only increases
// efficiency
// begin(), end(), rbegin(), rend(), size(),
// empty(), and swap() are same as those of about
// {1,2,3,4,5}
auto it = st.find(3);
//{1,2,3,4,5}
auto it = st.find(6);
//{1,4,5}
st.erase(5); // erases 5 //takes logarithmic time
int cnt = st.count(1);
auto it = st.find(3);
st.erase(it); // it take constant time
//{1,2,3,4,5}
auto it1 = st.find(2);
auto it2 = st.find(4);
st.erase(it1, it2); //after erase {1,4,5} [first, last]
// lower_bound() and upperA_bound() function works in the same way
//as in vector it dose.
//This in the syntax
auto it = st.lower_bound(2); //https://www.youtube.com/watch?v=edJ19qIL8WQ&t=0s
auto it = st.upper_bound(3); //https://www.youtube.com/watch?v=edJ19qIL8WQ&t=0s
}
void explainMultiSet(){
//Everything is same as set
//only stores duplicate elements also
multiset<int>ms;
ms.insert(1); // {1}
ms.insert(1); // {1,1}
ms.insert(1); // {1,1,1}
ms.insert(1); // all 1's erased
//only a single one erased
int cnt = ms.count(1);
ms.erase(ms.find(1), ms.findd(1)+2);
//rest all function same fas set
}
void explainUSet() {
unordered_set<int> st;
// lower_bound and upper_bound function
// does not works, rest all functions are same
// as above, it does not stores in any
// particular order it has a better complexity
// than set in most cases, except some when collision happens
}
void explainMap(){
map<int, int> mpp;
map<int, pair<int, >> mpp;
map<pai <int, int>, int>mpp;
mpp[1] = 2;
mpp.emplace({3, 1});
mpp.insert({2,4});
mpp[{2,3}] = 10;
{
{1, 2}
{2, 4}
{3, 1}
}
for (auto it: mpp){
cout << it.first << " " << it.second << endl;
}
cout << mpp[1];
cout << mpp[5];
}
void explainMultiMap() {
// everything same as map, only it can store multiple keys
// only mpp[key] cannot be used here
}
void explainUnorderedMap(){
//same as set and unordered_set difference.
}
bool comp(pair<int, int>p1, pair<int, int>p2){
if(p1.second < p2.second){
return true;
}else if(p1.second == p2.second){
if (p1.first > p2.second){
return true;
}
}
}
boll comp(pair<int,int> p1, pair<int,int>p2){
if(p1.second < p2.second) return true;
if(p1.second > p2.second) return false;
// they are same
if(p1.first > p2.first) return true;
return false;
}
void explainExtra(){
sort(a,a+n);
sort(v.begin(), v.end());
sort(a+2, a+4);
sort(a, a+n, greater<int>);
pair(a, a+n, greater<int>);
pair<int,int> a[] = {{1,2}, {2,1}, {4,1}};
// sort it according to second element
// if second element is same, then sort
// it according to first element but in descending
sort(a, a+n, comp);
//{4,1}, {2,1},{1,2};
int num = 7;
int cnt = __builtin_popcount();
long long num = 123456754333;
int num = __builtin_popcountll();
string s = "123";
do{
cout << s << endl;
}while(next_permutation(s.begin(), s.end()));
int maxi = *max_element(a,a+n);
}
int main()
{
// explainPair();
explainVector();
}