Skip to content

Commit dfe6c0b

Browse files
committed
STL stuffs
1 parent b0f17d4 commit dfe6c0b

5 files changed

+677
-0
lines changed

Data Structure/14 STL Set.cpp

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
2+
/**
3+
4+
SET
5+
===
6+
7+
Operations
8+
----------
9+
10+
1. insert : O(logN) :
11+
2. begin : O(1) : points to the first element
12+
3. rbegin : O(1) : points to the last element
13+
4. erase(iterator) : O(1) :
14+
erase(value) : O(logN) :
15+
erase(it1 , it2) : O(distance between it1 and it2) : deletes all value in range [it1,it2)
16+
5. swap : O(1)
17+
18+
6. find : O(logN) : returns iterator to st.end() if not present
19+
7. count : O(logN) : returns 1 if present , 0 otherwise
20+
21+
8. lower_bound(val) : O(logN) : returns VALUE of first element >= val
22+
9. upper_bound(val) : O(logN) : returns VALUE of first element > val
23+
24+
Note : There's NO WAY to know the position using lower_bound and upper_bound in CONSTANT TIME. We have to use PBDS for that.
25+
26+
27+
**/
28+
/** Which of the favors of your Lord will you deny ? **/
29+
30+
#include<bits/stdc++.h>
31+
using namespace std;
32+
33+
#define LL long long
34+
#define PII pair<int,int>
35+
#define PLL pair<LL,LL>
36+
#define MP make_pair
37+
#define F first
38+
#define S second
39+
#define INF INT_MAX
40+
41+
#define ALL(x) (x).begin(), (x).end()
42+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
43+
#define READ freopen("alu.txt", "r", stdin)
44+
#define WRITE freopen("vorta.txt", "w", stdout)
45+
46+
#include <ext/pb_ds/assoc_container.hpp>
47+
#include <ext/pb_ds/tree_policy.hpp>
48+
using namespace __gnu_pbds;
49+
50+
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
51+
52+
/**
53+
54+
PBDS
55+
-------------------------------------------------
56+
1) insert(value)
57+
2) erase(value)
58+
3) order_of_key(value) // 0 based indexing
59+
4) *find_by_order(position) // 0 based indexing
60+
61+
**/
62+
63+
template<class T1, class T2>
64+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
65+
template <class T>
66+
ostream &operator <<(ostream &os, vector<T>&v);
67+
template <class T>
68+
ostream &operator <<(ostream &os, set<T>&v);
69+
70+
inline void optimizeIO()
71+
{
72+
ios_base::sync_with_stdio(false);
73+
cin.tie(NULL);
74+
}
75+
76+
const int nmax = 2e5+7;
77+
const LL LINF = 1e17;
78+
79+
template <class T>
80+
string to_str(T x)
81+
{
82+
stringstream ss;
83+
ss<<x;
84+
return ss.str();
85+
}
86+
87+
//bool cmp(const PII &A,const PII &B)
88+
//{
89+
//
90+
//}
91+
92+
int main()
93+
{
94+
optimizeIO();
95+
srand(time(0));
96+
97+
vector<int>v;
98+
set<int>st , st1 , st2;
99+
100+
for(int i=0;i<15;i++)
101+
{
102+
int val = rand()%25;
103+
v.push_back(val);
104+
st.insert(val);
105+
106+
if(i&1) st1.insert(val);
107+
else st2.insert(val);
108+
}
109+
110+
cout<<v<<endl;
111+
cout<<st<<endl;
112+
113+
cout<<"First element : "<<*st.begin()<<endl;
114+
cout<<"Last element : "<<*st.rbegin()<<endl;
115+
116+
/** ERASE **/
117+
cout<<"ERASE"<<endl;
118+
119+
// erase using iterator
120+
auto it = st.begin();
121+
it++;
122+
it++; // iterator to 3rd element
123+
st.erase(it);
124+
cout<<st<<endl;
125+
126+
// erase range using iterator
127+
it = st.begin();
128+
for(int i=0;i<2;i++) it++;
129+
st.erase(st.begin(),it);
130+
cout<<st<<endl;
131+
132+
// erase using value
133+
st.erase(22);
134+
cout<<st<<endl;
135+
136+
/** SWAP **/
137+
cout<<"SWAP"<<endl;
138+
139+
cout<<st1<<endl;
140+
cout<<st2<<endl;
141+
142+
st1.swap(st2);
143+
144+
cout<<st1<<endl;
145+
cout<<st2<<endl;
146+
147+
/** FIND **/
148+
cout<<"FIND"<<endl;
149+
cout<<st<<endl;
150+
151+
it = st.find(19);
152+
if(it!=st.end()) cout<<"PRESENT"<<endl;
153+
else cout<<"ABSENT"<<endl;
154+
155+
/** COUNT **/
156+
cout<<"COUNT"<<endl;
157+
158+
if(st.count(19)!=0) cout<<"PRESENT"<<endl;
159+
else cout<<"ABSENT"<<endl;
160+
161+
/** LOWER BOUND UPPER BOUND **/
162+
163+
cout<<st<<endl;
164+
auto itlo = st.lower_bound(10);
165+
auto ithi = st.upper_bound(10);
166+
167+
cout<<"Lower bound value : "<<*itlo<<endl;
168+
cout<<"Upper bound value : "<<*ithi<<endl;
169+
170+
return 0;
171+
}
172+
173+
/**
174+
175+
**/
176+
177+
template<class T1, class T2>
178+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
179+
{
180+
os<<"{"<<p.first<<", "<<p.second<<"} ";
181+
return os;
182+
}
183+
template <class T>
184+
ostream &operator <<(ostream &os, vector<T>&v)
185+
{
186+
os<<"[ ";
187+
for(int i=0; i<v.size(); i++)
188+
{
189+
os<<v[i]<<" " ;
190+
}
191+
os<<" ]";
192+
return os;
193+
}
194+
195+
template <class T>
196+
ostream &operator <<(ostream &os, set<T>&v)
197+
{
198+
os<<"[ ";
199+
for(T i:v)
200+
{
201+
os<<i<<" ";
202+
}
203+
os<<" ]";
204+
return os;
205+
}
206+
207+

Data Structure/15 STL Map.cpp

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
2+
/**
3+
4+
MAP
5+
===
6+
7+
Declaration
8+
-----------
9+
10+
map< KEY , VALUE > MAP
11+
12+
Different type of possible variations of declaring MAP .
13+
14+
1. map<int,int>MAP1;
15+
2. map<PII,int>MAP2;
16+
3. map<string, vector<int> >MAP3;
17+
18+
There can be many many more ways of declaring maps
19+
20+
Operations
21+
----------
22+
23+
ALMOST identical to all the functions of STL SET.
24+
25+
26+
**/
27+
28+
/** Which of the favors of your Lord will you deny ? **/
29+
30+
#include<bits/stdc++.h>
31+
using namespace std;
32+
33+
#define LL long long
34+
#define PII pair<int,int>
35+
#define PLL pair<LL,LL>
36+
#define MP make_pair
37+
#define F first
38+
#define S second
39+
#define INF INT_MAX
40+
41+
#define ALL(x) (x).begin(), (x).end()
42+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
43+
#define READ freopen("alu.txt", "r", stdin)
44+
#define WRITE freopen("vorta.txt", "w", stdout)
45+
46+
#include <ext/pb_ds/assoc_container.hpp>
47+
#include <ext/pb_ds/tree_policy.hpp>
48+
using namespace __gnu_pbds;
49+
50+
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
51+
52+
/**
53+
54+
PBDS
55+
-------------------------------------------------
56+
1) insert(value)
57+
2) erase(value)
58+
3) order_of_key(value) // 0 based indexing
59+
4) *find_by_order(position) // 0 based indexing
60+
61+
**/
62+
63+
template<class T1, class T2>
64+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
65+
template <class T>
66+
ostream &operator <<(ostream &os, vector<T>&v);
67+
template <class T>
68+
ostream &operator <<(ostream &os, set<T>&v);
69+
70+
inline void optimizeIO()
71+
{
72+
ios_base::sync_with_stdio(false);
73+
cin.tie(NULL);
74+
}
75+
76+
const int nmax = 2e5+7;
77+
const LL LINF = 1e17;
78+
79+
template <class T>
80+
string to_str(T x)
81+
{
82+
stringstream ss;
83+
ss<<x;
84+
return ss.str();
85+
}
86+
87+
//bool cmp(const PII &A,const PII &B)
88+
//{
89+
//
90+
//}
91+
92+
93+
int main()
94+
{
95+
optimizeIO();
96+
97+
cout<<"Map Type 1 "<<endl;
98+
cout<<"==========="<<endl;
99+
100+
map<int,int>MAP1;
101+
MAP1[10] = 5;
102+
MAP1[5] = 15;
103+
MAP1[2] = 45;
104+
105+
for(auto x:MAP1)
106+
cout<<x.F<<" : "<<x.S<<endl;
107+
108+
cout<<"Map Type 2 "<<endl;
109+
cout<<"==========="<<endl;
110+
111+
map<PII,int>MAP2;
112+
MAP2[ {10,5} ] = 5;
113+
MAP2[ {5,2} ] = 15;
114+
MAP2[ {2,12} ] = 45;
115+
116+
for(auto x:MAP2)
117+
cout<<x.F.F<<" , "<<x.F.S<<" : "<<x.S<<endl;
118+
119+
cout<<"MAP Type 3 "<<endl;
120+
cout<<"==========="<<endl;
121+
122+
map<string, vector<int> >MAP3;
123+
MAP3["A"].push_back(15);
124+
MAP3["A"].push_back(5);
125+
126+
MAP3["B"].push_back(10);
127+
MAP3["B"].push_back(2);
128+
129+
for(auto x:MAP3)
130+
{
131+
cout<<x.F<<" : ";
132+
for(auto v:x.S)
133+
cout<<v<<" ";
134+
cout<<endl;
135+
}
136+
137+
return 0;
138+
}
139+
140+
/**
141+
142+
**/
143+
144+
template<class T1, class T2>
145+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
146+
{
147+
os<<"{"<<p.first<<", "<<p.second<<"} ";
148+
return os;
149+
}
150+
template <class T>
151+
ostream &operator <<(ostream &os, vector<T>&v)
152+
{
153+
os<<"[ ";
154+
for(int i=0; i<v.size(); i++)
155+
{
156+
os<<v[i]<<" " ;
157+
}
158+
os<<" ]";
159+
return os;
160+
}
161+
162+
template <class T>
163+
ostream &operator <<(ostream &os, set<T>&v)
164+
{
165+
os<<"[ ";
166+
for(T i:v)
167+
{
168+
os<<i<<" ";
169+
}
170+
os<<" ]";
171+
return os;
172+
}
173+
174+

0 commit comments

Comments
 (0)