-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSTL.cpp
More file actions
261 lines (198 loc) · 5.06 KB
/
Copy pathSTL.cpp
File metadata and controls
261 lines (198 loc) · 5.06 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
// STL
#include<iostream>
#include <bits/stdc++.h>
#include<vector>
#include<set>
#include<map>
using namespace std;
bool f(int x,int y){
return x > y;
}
void vectorDemo(){
vector<int> A = { 11, 2, 3, 14 };
//cout<< A[1]<<endl;
sort(A.begin(),A.end()); // O(NlogN)
/* 2,3,11,14 */
bool present = binary_search(A.begin(), A.end(), 3); //(start,end,value to be searched)
present = binary_search(A.begin(), A.end(), 4); //false
A.push_back(100);
present = binary_search(A.begin(),A.end(),100); // true
/* 2,3,11,14,100 */
A.push_back(100);
A.push_back(100);
A.push_back(100);
A.push_back(100);
A.push_back(100);
A.push_back(123);
//2,3,11,14,100*, 100,100,100,100,123**
/*
vector<int>::iterator it = lower_bound(A.begin(), A.end(), 100); // >=
// return 100*
vector<int>::iterator it2 = upper_bound(A.begin(),A.end(), 100); // >
// return 123**
cout << *it <<" "<< *it2 << endl;
cout<< it2 - it <<endl;
*/
// number of time 100 is occuring
// int count = it2 - it - 1;
// Sort in reverse order
sort(A.begin(),A.end(),f);
vector<int>::iterator it3;
for(it3 = A.begin();it3 != A.end();it3++){
cout<< *it3 <<" ";
}
cout<<endl;
// In this vector valus wont be changed
for(int x:A){
cout<< x <<" ";
}
auto it = lower_bound(A.begin(), A.end(), 100); // >=
auto it2 = upper_bound(A.begin(),A.end(), 100); // >
// For handling values
// to change values in vector iterate with reference
for(int &x: A){ // reference
x++;
cout<< x<<" ";
}
}
void setDemo(){
// In set every operation insertion and deletion takes Log(n) time
// but in this the elements are always sorted at any point of time
set<int> S;
S.insert(1);
S.insert(2);
S.insert(-1);
S.insert(-10);
for(int x: S){
cout<< x << " ";
}
cout<<endl;
// -10 -1 1 2
auto it = S.find(-1);
if( it == S.end() ){
cout<<"Element not present\n";
}else{
cout<<"Element is present\n";
cout<< *it <<endl;
}
/* set of pair
std::pair<int,int> p1(1,0);
std::pair<int,int> p2(2,1);
std::set<std::pair<int,int>> s;
s.insert(p1);
s.insert(p2);
auto it = s.find(p1);
std::cout << it->first << "," << it->second <<std::endl;
*/
// Set In Decreasing order
set<int,greater <int>> elements;
// Lower bound and upper bound
auto it2 = S.upper_bound(-1);
auto it3 = S.upper_bound(0);
cout<< *it2 << " " << *it3 << endl;
auto it4 = S.upper_bound(2);
if(it4 == S.end()){
cout<<"oops! Cant find something like that\n";
}
// Erasing a element
cout<<"\nBefore Erasing:\n";
for(int x:S){
cout<<x<<" ";
}
S.erase(1);
cout<<endl<<"After Erasing\n";
for(int x:S){
cout<<x<<" ";
}
// convert set to a vector
set<int> values;
vector<int> ans(values.begin(),values.end());
}
void mapDemo(){
map<int,int> A;
A[1] = 100;
A[2] = -1;
A[3] = 200;
A[12837] = 1;
map<char, int>cnt;
string x = "Aditya Mahajan";
for(char c:x){
cnt[c]++;
}
cout<< cnt['a'] << " " << cnt['z'] <<endl;
// ============================================
std::map <string,int > Map;
Map["Chotu"] = 90939208;
Map["Amit"] = 82396497;
Map.insert(std::make_pair("Bot",28736872));
// Traversing the Map
for(auto el1: Map){
cout << el1.first << " " << el1.second <<endl;
}
// Access using [] Operator
cout<< Map["Chotu"] << endl;
// ============================================
// Frequency Counter
map<int,int> mp;
for(int i=0;i<10;i++){
int x;
cin>>x;
mp[x]++;
}
for(auto ele:mp){
cout<<ele.first<<" "<<ele.second<<endl;
}
// ============================================
}
void powerofStl(){
// [x,y]
/*
add[2,3]
add[10,20]
add[30,400]
add[401,450]
give me the interval 401
*/
set< pair<int,int> > S;
S.insert({2,3});
S.insert({10,20});
S.insert({30,400});
S.insert({401,450});
/*
comparing 2 pairs
pair {a,b} is smaller than pair {c,d}
iff (a<c) then
{a,b} < {c,d}
or if(a==c and b<d)
{a,b} < {c,d}
*/
//2,3
//10,20
//30,400
//401,450
int point = 50;
auto it = S.upper_bound({point,INT_MAX});
if(it == S.begin()){
cout<<"The point is not lying in any interval.. \n";
return;
}
it--;
pair<int,int> current = *it;
if(current.first <= point && point <= current.second){
cout<<"yes its present:" <<current.first <<" "<<current.second <<endl;
}else{
cout<<"The given point is not lying in any interval.. \n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
/*
sets, vector
sort vector of structs,set of structs
*/
//setDemo();
cout<<endl;
return 0;
}