-
Notifications
You must be signed in to change notification settings - Fork 6
/
Libraries and Functions
293 lines (209 loc) · 4.29 KB
/
Libraries and Functions
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
C++ Libraries and Usages
------------------------
------------------------
Iostream
--------
cstdio
------
1. Input from file via stdin
freopen(FILENAME, "r", stdin)
string
------
string str;
1. String length
str.length()
2. Get string till new line
getline(cin, str)
3. Int to string
to_string(int)
4. Str to Int
stoi(string)
Cstring
-------
char *s; #null terminated string
1. String length
strlen(s);
2. initialize string/array
memset(int *ptr, value, sizeof(ptr))
ex. memset(myarr, 0, sizeof(myarr))
3. initialize with character
string str(LEN, CHAR);
sstream
-------
1. split string
climits
-------
INT_MAX 2147483647
INT_MIN -2147483648
UINT_MAX 4294967295
LONG_MAX 9223372036854775807
ULONG_MAX 18446744073709551615
cmath
-----
1. Absolute of number
abs(INT)
2. Max of two number
max(a, b);
stack
-----
stack<int> s;
1. Push
s.push(INT)
2. Pop
s.pop()
3. Top Element
INT = s.top()
4. Check if stack is empty
BOOL = s.empty()
5. Size of stack
INT = s.size()
6. Swap two stack
s.swap(s1)
queue
-----
queue<int> q;
1. Push
q.push(INT)
2. Pop
q.pop()
3. Front Element
INT = q.front()
4. Back Element
INT = q.back()
5. Check if stack is empty
BOOL = q.empty()
6. Size of stack
INT = q.size()
7. Swap two queue
q.swap(q1)
priority_queue
--------------
priority_queue<int> pq;
1. Push
pq.push(INT)
2. Pop
pq.pop()
3. Top Element
INT = pq.top()
4. Check if stack is empty
BOOL = pq.empty()
5. Size of stack
INT = pq.size()
6. Swap two stack
pq.swap(pq1)
Vector
------
vector<type> vec (n) #initialize n elements with 0
1. Size of vector
vec.size()
2. Push element at back
vec.push_back()
3. Pop element from back
vec.pop_back()
4. Return vector iterator of begining
vector<type>::iterator it = vec.begin()
5. Return vector iterator of end
vector<type>::iterator it = vec.end()
6. check if vector is empty
vec.empty()
7. Access front element
vec.front()
8. Access back element
vec.back()
9. Resize or set size of vector
vec.resize(WIDTH)
List
----
list<type> my
1. Size of vector
my.size()
2. Push element at back
my.push_back()
3. Push element at front
my.push_front()
4. Pop element from back
my.pop_back()
5. Pop element from back
my.pop_back()
6. Return list iterator of begining
list<type>::iterator it = my.begin()
7. Return list iterator of end
list<type>::iterator it = my.end()
6. check if vector is empty
my.empty()
7. Access front element
my.front()
8. Access back element
my.back()
9. Insert element
my.insert(it, VALUE)
10. Remove element
my.remove(VALUE)
11. Sort element
my.sort() # put Upper Case obove than lower case
12. Merger elements
my.merge(my1)
Map
---
map<key, type> my
1. Size of map
my.size()
2. Return map iterator of begining
map<key, type>::iterator it = my.begin()
3. Return map iterator of end
map<key, type>::iterator it = my.end()
4. Insert elements
my.insert(pair<key, type>(KEY, VALUE));
my[KEY] = VALUE;
5. Clear all the pair
my.clear();
6. Erase element having key
it = my.find(KEY)
my.erase(it)
7. check if map is empty
my.empty()
8. Size of map
my.size()
9. Max size a map can hold
my.max_size()
10. Find element
it = my.find(KEY)
11. Count elements in Key
my.count()
Algorithm
---------
1. Sort vector array
sort(vec.begin(), vec.end(), compare)
2. Make heap
make_heap(vec.begin(), vec.end(), compare)
3. Push heap
vec.push_back(INT)
push_heap(vec.begin(), vec.end())
4. Pop heap
pop_heap(vec.begin(), vec.end())
vec.pop_back();
5. Fill array/vector
fill(array, array+n, INT);
6. Find Upper bound
upper_bound(vec.begin(), vec.end(), INT) // Return pointer
7. Find Lower bound
lower_bound(vec.begin(), vec.end(), INT)
Climits
-------
1. int range/ long int range
-2147483648 to 2147483647
INT_MAX = (1<<31)-1 ~ 2*10^9
INT_MIN = (1<<31) ~ -2*10^9
2. short int range
-32768 to 32767
SHRT_MAX = (1<<15)-1 = 32767 ~ 3*10^4
SHRT_MIN = -(1<<15) = -32768 ~ -3*10^4
3. unsigned int range
0 to 4294967295
4. unsigned int / unsigned long int
0 to 4,294,967,295
4. unsigned short int range
0 to 65,535
5. long long int
LLONG_MAX = ~ 9*10^18
LLONG_MIN = ~ -9*10^18