Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61ddd7a

Browse files
Merge pull request #92 from Geek-Tekina/main
Added Codes for multiple containers of STL
2 parents d19c83c + 89ec673 commit 61ddd7a

File tree

12 files changed

+269
-0
lines changed

12 files changed

+269
-0
lines changed

‎STL/2d_Vector.cpp‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 2D vector
2+
#include<iostream>
3+
#include<vector>
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
int main(){
7+
8+
vector <vector <int>> v; //declaration of 2d vector
9+
vector <int> v1;
10+
int i;
11+
for( i=1;i<=5;++i)
12+
{
13+
v1.push_back(i*10);
14+
}
15+
vector <int> v2;
16+
for(;i<=10;++i)
17+
{
18+
v2.push_back(i*10);
19+
}
20+
vector <int> v3;
21+
for(;i<=15;++i)
22+
{
23+
v3.push_back(i*10);
24+
}
25+
v.push_back(v1); //pushing vectors as a element in 2d vector
26+
v.push_back(v2);
27+
v.push_back(v3);
28+
for(auto o_it:v)
29+
{
30+
for(auto i_it:o_it)
31+
{
32+
cout << i_it<<" ";
33+
}
34+
cout<<endl;
35+
}
36+
return 0;
37+
}

‎STL/Array.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//First Container : Array
2+
//Array stores element in contagious memory locations.
3+
#include <iostream>
4+
#include <array>
5+
using namespace std;
6+
int main()
7+
{
8+
array<int, 5> arr = {1, 2, 3, 4, 5}; //declaration
9+
for (auto it : arr) //printing using auto iterator
10+
{
11+
cout << it << " ";
12+
}
13+
cout << endl
14+
<< arr.at(0) << endl; //printing elemnt at 0 using at func
15+
for (auto it2 = arr.begin(); it2 != arr.end(); ++it2) //printing using begin() and end()
16+
{
17+
cout << *it2 << " ";
18+
}
19+
cout << endl;
20+
for (auto it3 = arr.rbegin(); it3 != arr.rend(); ++it3) //printing in reverse order using rbegin() and rend()
21+
{
22+
cout << *it3 << " ";
23+
}
24+
cout << endl;
25+
cout << arr.front(); // printing first and last element
26+
cout << " " << arr.back();
27+
cout<<"\nSize of array :"<<arr.size()<<endl;
28+
array<int ,10> arr1;
29+
arr1.fill(10); //filling array with fill() fnc
30+
for (auto it : arr1) //printing using auto iterator
31+
{
32+
cout << it << " ";
33+
}
34+
return 0;
35+
}

‎STL/Vector.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//Second Container : Array
2+
//Vector ~ Dynamic Array
3+
#include <iostream>
4+
#include <vector>
5+
using namespace std;
6+
int main()
7+
{
8+
vector<int> v; //declaration
9+
v.push_back(1); //insertion using pop_back fnc
10+
v.push_back(2);
11+
v.push_back(3);
12+
v.push_back(4);
13+
v.push_back(5);
14+
for (auto it : v) //printing using auto iterator
15+
{
16+
cout << it << " ";
17+
}
18+
cout << endl;
19+
for (auto it2 = v.begin(); it2 != v.end(); ++it2) //printing using begin() and end()
20+
{
21+
cout << *it2 << " ";
22+
}
23+
cout << endl;
24+
cout << "Size of vector : " << v.size();
25+
v.pop_back(); //popping out last element
26+
cout << endl
27+
<< "Capacity of vector : " << v.capacity() << endl;
28+
vector<int> v2;
29+
v2 = v; //copying v vector into v2
30+
for (auto it : v2) //printing using auto iterator
31+
{
32+
cout << it << " ";
33+
}
34+
v.emplace_back(5); //entering value using emplace back fnc
35+
return 0;
36+
}

‎STL/calculator_using_templatefnc.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<iostream>
2+
using namespace std;
3+
template < typename T>
4+
void calculator(T a,T b){
5+
cout<<"Sum of input numbers: "<<a+b<<endl;
6+
cout<<"Multiply of input numbers: "<<a*b<<endl;
7+
cout<<"Division of input numbers: "<<a/b<<endl;
8+
}
9+
int main()
10+
{
11+
calculator <int>(2,3);
12+
13+
calculator <float>(2.2,3.3);
14+
15+
calculator <double>(2.2222,3.333);
16+
}
17+

‎STL/compare_sortfnc.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
#include<utility>
4+
#include<vector>
5+
using namespace std;
6+
bool myCmp(pair<string, int> p1, pair<string, int> p2)
7+
{
8+
if(p1.second > p2.second)
9+
{
10+
return 1;
11+
}
12+
else if(p1.second == p2.second)
13+
{
14+
if(p1.first < p2.first)
15+
{
16+
return 1;
17+
}
18+
else
19+
{
20+
return 0;
21+
}
22+
}
23+
24+
return 0;
25+
}
26+
int main(){
27+
vector <pair<string,int>> v ={{"ram",89},{"shyam",91},{"pyam",89}};
28+
sort(v.begin(),v.end(),myCmp);
29+
for(auto i=0;i<v.size();++i){
30+
cout<<v[i].first<<" "<<v[i].second<<endl;
31+
}
32+
return 0;
33+
}

‎STL/deque.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<iostream>
2+
#include<deque> //allows insertion and deletion at both ends
3+
using namespace std;
4+
int main(){
5+
deque <int> d;
6+
d.push_back(11);cout<<d.size()<<" ";
7+
d.push_front(22);cout<<d.size()<<" ";
8+
d.push_front(32);cout<<d.size()<<" ";
9+
d.push_back(44);cout<<d.size()<<" ";
10+
d.pop_front();cout<<d.size()<<" ";
11+
for(int i=0;i<d.size();++i){
12+
cout<<d[i]<<" ";
13+
}
14+
15+
cout<<d.size();
16+
// cout<<d.front();
17+
// cout<<" "<<d.back();
18+
19+
return 0;
20+
}

‎STL/list.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include<iostream>
2+
#include<list>
3+
using namespace std;
4+
int main(){
5+
list <int> l;
6+
l.push_back(23);
7+
l.push_front(34);
8+
for (int i:l){
9+
cout<<i;
10+
}
11+
return 0;
12+
}

‎STL/map.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// it maps keys to values
2+
//returns keys in sorted order
3+
//program to map number to sqr
4+
#include <iostream>
5+
#include <map>
6+
using namespace std;
7+
int main()
8+
{
9+
map<int, int> m;
10+
m[1] = 1; //here 1 in brackets is key and 1 after is its mapped value
11+
m[2] = 4;
12+
m.insert({-1, 1});
13+
m[3] = 9;
14+
m[10] = 100;
15+
m.insert({-3, 9}); //same -1 is key and 1 is the value
16+
for (auto i : m)
17+
{
18+
cout << i.first << "-->" << i.second << endl; //this will print in sorted order
19+
}
20+
21+
cout << "Finding -1 " << m.count(-1) << endl; //returns bool value : 1 if present 0 if not
22+
cout << "Finding -6 " << m.count(-6) << endl;
23+
//This particular block searches for a key and then print the map starting from that key neglecting the key less than it. for ex in this case if we want to see just the positive mapping we can do it in this manner.
24+
{
25+
auto it = m.find(1);
26+
for (auto i = it; i != m.end(); ++i)
27+
{
28+
cout << (*i).first << "-->" << (*i).second << endl;
29+
}
30+
}
31+
return 0;
32+
}

‎STL/pair.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
int main()
5+
{
6+
pair<int, string> p; // Declaration
7+
p = make_pair(2, "ABCD"); //initialisation
8+
// We can also use cin to take .first and .second value in the vector.
9+
cout << p.first << " " << p.second;
10+
cout << endl;
11+
int a[3] = {1, 2, 3};
12+
int b[3] = {11, 22, 33};
13+
pair<int, int> q[3]; //Declaration of pair array
14+
q[0] = {a[0], b[0]};
15+
q[1] = {a[1], b[1]};
16+
q[2] = {a[2], b[2]};
17+
for (int i = 0; i < 3; ++i)
18+
{
19+
cout << q[i].first << " " << q[i].second << endl;
20+
}
21+
22+
pair<string, string> s;
23+
getline(cin, s.first);
24+
getline(cin, s.second);
25+
cout << s.first << " " << s.second;
26+
27+
return 0;
28+
}

‎STL/priority_queue.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include<iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
int main(){
5+
priority_queue<int> pq;
6+
pq.push( 1);
7+
pq.push(6);
8+
pq.push(4);
9+
pq.push(-1);
10+
cout<<pq.top();
11+
pq.pop();
12+
cout<<endl;
13+
cout<<pq.top();
14+
return 0;
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /