forked from Muhammad-Magdi/problem-solving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
STL(1 of 2).txt
193 lines (184 loc) · 4.36 KB
/
STL(1 of 2).txt
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
What to know?
1- When to use.
2- Its Features.
3- Its member Functions.
4- How to initialize?
5- How to iterate?
6- How does it work?
- What if we want to store a user name and his ID? What if we want to sort an array of them?
- A Cartesian point.
Pair<T, T> p;
- pair<int, int> p = {1, 7};
- pair<int, int> p = make_pair(3, -2);
- pair<string, int> p("Ali", 109);
- pair<int, int> p = pair<int, int>(5, 7);
- make_pair(f, s) ... {f, s}
- first, second
Sequence containers:
-What if we don't know the size of the array -Not Even the exact maximum- ?
vector<T> v;
Features:
- Opened from the back.
- Dynamic size.
- Constant access time.
- [] operator.
Initialization:
- vector<int> v(size); //initialized by Zeros
- vector<int> v(size, val);
- vector<int> v2(v1);
- vector<int> v2(begin, end);
- vector<int> v = vector<int>();
Commonly used Member functions:
- begin()
- end()
- rbegin()
- rend()
- resize(size)
- reserve(capacity) **
- size()
- empty()
- push_back(val)
- pop_back()
- back()
- front()
- insert(it, val) --- insert(it, begin, end)
- erase(it) --- erase(begin, end)
- clear()
Iteration:
for(int i = 0 ; i < n ; ++i){
scanf("%d", &x);
v.push_back(x);
}
for(int i = 0 ; i < n ; ++i)
printf("%d ", v[i]);
for(int x : v)
printf("%d ", x);
for(auto x = v.begin() ; x!=v.end() ; ++x)
printf("%d ", *x);
-What if we want to insert in the beginning?
deque<T> dq;
Features:
- Opened from both ends.
- Dynamic size.
- Constant access time.
- [] operator.
Initialization:
- deque<int> dq(size); //initialized by Zeros
- deque<int> dq(size, val);
- deque<int> dq2(dq1);
- deque<int> dq2(begin, end);
- deque<int> dq = deque<int>();
Commonly used Member functions:
- begin()
- end()
- rbegin()
- rend()
- resize(size)
- size()
- empty()
- push_back(val)
- pop_back()
- back()
- push_front(val)
- pop_front()
- front()
- insert(it, val) --- insert(it, begin, end)
- erase(it) --- erase(begin, end)
- clear()
Iteration:
for(int i = 0 ; i < n ; ++i){
scanf("%d", &x);
dq.push_back(x);
}
for(int i = 0 ; i < n ; ++i)
printf("%d ", dq[i]);
for(int x : dq)
printf("%d ", x);
for(auto x = dq.begin() ; x!=dq.end() ; ++x)
printf("%d ", *x);
list<T> l;
Features:
- Constant insertion time.
- Dynamic size.
Initialization:
- list<int> l(size); //initialized by Zeros
- list<int> l(size, val);
- list<int> l2(l1);
- list<int> l2(begin, end);
- list<int> l = list<int>();
Commonly used Member functions:
- begin()
- end()
- rbegin()
- rend()
- resize(size)
- size()
- empty()
- push_back(val)
- pop_back()
- back()
- push_front(val)
- pop_front()
- front()
- insert(it, val) --- insert(it, begin, end)
- erase(it) --- erase(begin, end)
- clear()
- sort()
- splice(it, list) --- splice(it, list, begin, end) //Cut and paste
- remove(val)
- merge(list) //Merges sorted lists
- unique()
Iteration:
for(int i = 0 ; i < n ; ++i){
scanf("%d", &x);
l.push_back(x);
}
for(int x : l)
printf("%d ", x);
for(auto x = l.begin() ; x!=l.end() ; ++x)
printf("%d ", *x);
Container Adapters:
stack<T> st; //LIFO
Features:
- deque with some restrictions.
- Last in first out.
Commonly used Member functions:
- size()
- empty()
- push(val)
- pop() //Avoid RTE
- top() //Avoid RTE
Iteration:
while(!st.empty()){
printf("%d ", st.top());
st.pop();
}
queue<T> q; //FIFO
Features:
- deque with some restrictions.
- First in first out.
Commonly used Member functions:
- size()
- empty()
- push(val)
- pop() //Avoid RTE
- front() //Avoid RTE
Iteration:
while(!q.empty()){
printf("%d ", q.front());
q.pop();
}
priority_queue<T> pq; //FIFO
Features:
- Max heap tree.
Commonly used Member functions:
- size()
- empty()
- push(val)
- pop() //Avoid RTE
- top() //Avoid RTE
Iteration:
while(!pq.empty()){
printf("%d ", pq.top());
pq.pop();
}