Skip to content

Commit 81ccc30

Browse files
Updated repo with new codes LC-STL
1 parent 006bef8 commit 81ccc30

29 files changed

+1188
-0
lines changed

C++/STL C++/STL_LEC_1_Array_1.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* if we declare an array and don't initialize values then in int main it will have garbage values
2+
where if we declare them in global space it will be initialized to zeroes */
3+
4+
/* If we initialize the first element with any number other numbers in the array will be zeroes */
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
array<int, 10> arr;
10+
11+
int main(){
12+
array<int, 10> arr_1;
13+
14+
// Printing the global array
15+
for(int i=0; i<10; i++){
16+
cout<< arr[i] << " ";
17+
}
18+
cout<<"\n";
19+
// Output:
20+
// 0 0 0 0 0 0 0 0 0 0
21+
22+
// Printing the local array
23+
for(int i=0; i<10; i++){
24+
cout<< arr_1[i] << " ";
25+
}
26+
cout<<"\n";
27+
// Output:
28+
// -1663042624 98 2095127112 32759 -802929584 559 -1663042384 1 213850828 142645788
29+
30+
// Printing a local array where we intialize half array & leave other half
31+
array<int, 10> arr_2 = {1,2,3};
32+
for(int i=0; i<10; i++){
33+
cout<< arr_2[i] << " ";
34+
}
35+
cout<<"\n";
36+
// Output:
37+
// 1 2 3 0 0 0 0 0 0 0
38+
39+
// Proving that initialzing the first element with any value makes all the values of the array zero
40+
array<int, 10> arr_3 = {0};
41+
for(int i=0; i<10; i++){
42+
cout<< arr_3[i] << " ";
43+
}
44+
cout<<"\n";
45+
// Output:
46+
// 0 0 0 0 0 0 0 0 0 0
47+
}

C++/STL C++/STL_LEC_1_Array_2.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
array.fill method
3+
<ArrayName>.fill(<value>)
4+
5+
Accessing elements using at
6+
<ArrayName>.at(<index>)
7+
*/
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
int main(){
13+
array<int, 10> arr;
14+
15+
// Before filling the array
16+
for(int i=0; i<10; i++){
17+
cout<< arr[i] << " ";
18+
}
19+
cout<<"\n";
20+
// Output:
21+
// 450756608 32758 0 2 782236016 23 450763352 32758 -1731603232 527
22+
23+
// Fill Method
24+
arr.fill(5);
25+
26+
// After filling
27+
// Accessing elements using .at()
28+
for(int i=0; i<10; i++){
29+
cout<< arr.at(i) << " ";
30+
}
31+
cout<<"\n";
32+
// Output:
33+
// 5 5 5 5 5 5 5 5 5 5
34+
}

C++/STL C++/STL_LEC_1_Iterators_1.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Iterators:
3+
begin() -> points to the first element of the array
4+
end() -> points after the last element of the array
5+
rbegin() -> points to the last element of the array
6+
rend() -> points before first element of the array
7+
*/
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
int main(){
13+
array<int, 5> arr = { 1, 3, 4, 5, 6};
14+
for(auto it = arr.begin(); it!=arr.end(); it++){
15+
cout << *it << " ";
16+
} cout << endl;
17+
18+
for(auto it = arr.rbegin(); it!=arr.rend(); it++){
19+
cout << *it << " ";
20+
} cout<<endl;
21+
22+
// also called for-each loop
23+
for(auto it: arr){
24+
cout << it << " ";
25+
} cout << endl;
26+
27+
string s = "arunkumar";
28+
for(auto c: s){
29+
cout << c << " ";
30+
} cout << endl;
31+
32+
cout << arr.size() << endl;
33+
cout << arr.front() << " " << arr.at(0) << endl;
34+
cout << arr.back() << " " << arr.at(arr.size()-1) << endl;
35+
}

C++/STL C++/STL_LEC_1_vector_1.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
// vector<int> arr;
6+
// arr.push_back(0);
7+
// arr.push_back(2);
8+
// cout << arr.size() << endl; // 2
9+
// arr.pop_back(); // element 2 got removed
10+
// cout << arr.size() << endl; // 1
11+
// arr.push_back(1); arr.push_back(5); arr.push_back(3); arr.push_back(4);
12+
// cout << arr.size() << endl; // 5
13+
14+
// // clear will delete all vector elements but will not delete the vector
15+
// arr.clear();
16+
// cout << arr.size() << endl; // 0
17+
18+
// what if we want to create an vector of size x
19+
// vector<int> ar(4,0); // {0 0 0 0} here 4 is the size and the following is the element with which the vector needs to initialized
20+
// vector<int> ar1(10,5); // {5 5 5 5 5 5 5 5 5 5} size 10 intialize all elements with 5
21+
22+
// copying a vector into another
23+
// vector<int> ar2(ar1); // {5 5 5 5 5 5 5 5 5 5}
24+
// vector<int> ar3 = {1,2,3,4,5,6};
25+
// vector<int> ar4(ar3.begin(), ar3.begin()+2); // {1,2} 2nd element is not inclusive [)
26+
27+
// emplace_back is faster than push_back
28+
vector<int> ar5;
29+
ar5.emplace_back(5);
30+
ar5.emplace_back(2);
31+
ar5.emplace_back(8);
32+
for(auto it=ar5.begin(); it != ar5.end(); it++){
33+
cout << *it << " ";
34+
}
35+
36+
}

C++/STL C++/STL_LEC_1_vector_2.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
// 2d array
6+
vector<vector<int>> arr;
7+
8+
vector<int> ar1;
9+
ar1.emplace_back(1);
10+
ar1.emplace_back(2);
11+
ar1.emplace_back(3);
12+
13+
vector<int> ar2;
14+
ar2.emplace_back(4);
15+
ar2.emplace_back(5);
16+
ar2.emplace_back(6);
17+
18+
vector<int> ar3;
19+
ar3.emplace_back(7);
20+
ar3.emplace_back(8);
21+
ar3.emplace_back(9);
22+
23+
arr.push_back(ar1);
24+
arr.push_back(ar2);
25+
arr.push_back(ar3);
26+
27+
for(auto vectr: arr){
28+
for(auto it: vectr){
29+
cout << it << " ";
30+
} cout << endl;
31+
}
32+
33+
// 2D vector 10x20
34+
vector<vector<int>> vec2d(10, vector<int> (20,0));
35+
36+
// 3D vector 10x20x30
37+
vector<vector<vector<int>>> vec3d(10, vector<vector<int>> (20, vector<int> (30, 0)));
38+
}

C++/STL C++/STL_LEC_2_Map_1.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// a map stores the values in key value pairs and orders them according to keys(alphabetically for example)
2+
// a unordered map stores values but are not ordered and keys are unique here
3+
// a multipmap is ordered and storing multiple duplicate keys is possible
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
int main(){
8+
map<string,int> mpp;
9+
mpp["arun"] = 17;
10+
mpp["sam"] = 25;
11+
mpp["gilly"] = 21;
12+
mpp["jon"] = 1;
13+
mpp["gilly"] = 21;
14+
15+
cout << "size: " << mpp.size() << endl; // 4
16+
cout << "Map is empty: " << mpp.empty() << endl;
17+
18+
mpp.erase(mpp.find("sam"));
19+
for(auto it: mpp){
20+
cout << it.first << " " << it.second << endl;
21+
}
22+
23+
// unordered map
24+
unordered_map<string, int> ump;
25+
//all functions more or less similar to map
26+
27+
multimap<string,int> mmp;
28+
//all functions more or less similar to map
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// PQ stores elements in descending order
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
int main(){
6+
cout << "Max PQ" << endl;
7+
priority_queue<int> pq;
8+
pq.push(1); pq.push(3); pq.push(8); pq.push(12);
9+
cout << pq.top() << endl; // 12
10+
pq.pop();
11+
cout << pq.top() << endl; // 8
12+
13+
cout << endl << "Min PQ" << endl;
14+
priority_queue<int,vector<int>,greater<int>> pq1;
15+
pq1.push(1); pq1.push(3); pq1.push(8); pq1.push(12);
16+
cout << pq1.top() << endl; // 1
17+
pq1.pop();
18+
cout << pq1.top() << endl; // 3
19+
}

C++/STL C++/STL_LEC_2_Questions_1.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// given N elements, print the elements that occurs maximum no . of times
2+
// input
3+
// 5
4+
// 1 3 3 3 2
5+
6+
// output
7+
// 3
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
int main(){
13+
int n, max=1;
14+
map<int,int> mpp;
15+
cin >> n;
16+
while(--n){
17+
int x;
18+
cin >> x;
19+
mpp[x]++;
20+
if(mpp[x] > mpp[max]){
21+
max = x;
22+
}
23+
}
24+
cout << "The Number with highest frequency is: " << mpp[max];
25+
}

C++/STL C++/STL_LEC_2_Questions_2.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// given N elements, print all the elements in sorted order
2+
// input
3+
// 6
4+
// 6 6 3 2 3 5
5+
6+
// output
7+
// 2 3 3 5 6 6
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
int main(){
13+
int n;
14+
multiset<int> ms;
15+
cin >> n;
16+
while(n--){
17+
int x;
18+
cin >> x;
19+
ms.insert(x);
20+
}
21+
for(auto it: ms){
22+
cout << it << " ";
23+
}
24+
}

C++/STL C++/STL_LEC_2_Set_1.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// A set has only unique elements
2+
// The first element is the smallest and the 2nd element is the 2nd smallest element (continued trend)
3+
// an unordered set has elements in random order (No ascending) and elements are still unique here
4+
// In a multiset the elements are stored in ascending fashion and we can have multiple instances of a same element
5+
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
int main(){
11+
// set<int> st;
12+
13+
// int n,x=0;
14+
// cin >> n;
15+
// for(int i=0; i<n; i++){
16+
// cin >> x;
17+
// st.insert(x);
18+
// }
19+
20+
// st.erase(st.begin()); // will remove the first element in the set (the smallesto one)
21+
// st.erase(5); // will remove the element specified
22+
23+
// new code below
24+
25+
// set<int> st1 = {5,2,1,4,3}; // the order doesnt matter it will be in ascending only
26+
// auto it1 = st1.find(2);
27+
// for(auto it=st1.begin(); it!= st1.end(); it++){
28+
// cout << *it << " ";
29+
// }
30+
// cout << "\nsize of the set before erase: " << st1.size() << endl;
31+
// st1.erase(it1); // iterator pointing to the element specified
32+
33+
// for(auto it=st1.begin(); it!= st1.end(); it++){
34+
// cout << *it << " ";
35+
// }
36+
// cout << endl << "size of the set after erase: " << st1.size() << endl;
37+
38+
//new code below
39+
// set<int> st2 = {5,2,1,4,3};
40+
// for(auto it: st2){
41+
// cout << it << " ";
42+
// }
43+
44+
// unordered set
45+
// unordered_set<int> ust;
46+
// unordered_set<int> ust1 = {9,4,1}; // 9 4 1
47+
// ust.emplace(5); // 5
48+
// ust.insert(21); // 5 21
49+
// ust.emplace(9); // 5 21 9
50+
51+
// ust.erase(21); // 9 5
52+
// ust1.erase(ust1.find(9)); // 4 1
53+
// for(auto it: ust1){
54+
// cout << it << " ";
55+
// }
56+
57+
// Multiset
58+
multiset<int> ms;
59+
ms.insert(8);
60+
ms.emplace(6);
61+
ms.insert(6);
62+
ms.emplace(5); // 5 6 6 8
63+
64+
ms.size(); // 4
65+
66+
ms.erase(ms.begin()); // 6 6 8
67+
ms.erase(ms.find(6)); // 6 8
68+
69+
ms.size(); // 2
70+
}

0 commit comments

Comments
 (0)