Skip to content

Instantly share code, notes, and snippets.

@i-msid
Created July 24, 2020 16:28
Show Gist options
  • Save i-msid/5792f3b77c6d8d57bcd664216a2f2a5f to your computer and use it in GitHub Desktop.
Save i-msid/5792f3b77c6d8d57bcd664216a2f2a5f to your computer and use it in GitHub Desktop.
// Pre Placement Revision Guide!
// All the very best for placements!
#include <bits/stdc++.h>
using namespace std;
/*
THIS RESOURCE WAS MADE FOR END MIN REVISION OF SOME INBUILT DS AND CONTAINERS THAT C++ STL
HAS TO OFFER.
IF THERE IS ANY BUG OR ERROR PLEASE LET ME KNOW!
*/
/*
LIST OF CONTAINERS:
1) sorting a vector
2) sorting a vector in reverse order
3) sorting a pair based upon second element of pair(in decreasing order)
4) max-heap
5) min-heap
6) set(it is a kind of binary tree)
7) multi-set
8) map
9) stack
10) queue
11) dequeue
*/
bool comp1(int &a, int &b)
{
return a > b;
}
bool comp3(pair<int, int> &a, pair<int, int> &b)
{
return (a.second > b.second); // sorts the vector in decreasing order acc to there second element
}
int main()
{
// 1) and 2) sorting a vector and sorting a vector in reverse order
vector<int> vec; //vector is like an dynamic size array
for (int i = 0; i < 5; i++)
{
vec.push_back(5 - i); // push_back is used to append elements at the back of vector
}
sort(vec.begin(), vec.end()); // sorts the vector in increasing order
cout << vec[0] << endl;
sort(vec.begin(), vec.end(), comp1); // sorts the vector in decreasing order
cout << vec[0];
// 3) sorting a pair based upon second element of pair(in decreasing order)
vector<pair<int, int>> pvec;
pvec.push_back(make_pair(5, 6)); // make pair is used for making pair of the form of (x,y)
pvec.push_back(make_pair(4, 5));
pvec.push_back(make_pair(3, 5));
pvec.push_back(make_pair(3, 6));
pvec.push_back(make_pair(2, 6));
sort(pvec.begin(), pvec.end(), comp3);
for (auto x : pvec)
{
cout << x.first << " " << x.second << endl;
}
// (4) max heap implimentation
cout << "MAX HEAP \n";
priority_queue<int> heap;
heap.push(10); // push is used to insert elements in a heap
heap.push(40);
heap.push(5);
cout << heap.top() << " "; // top is used to acess the top element
heap.pop(); // pop is used to delete the top element
cout << heap.top() << endl;
// (5) min heap implimentation
cout << "MIN HEAP :\n";
priority_queue<int, vector<int>, greater<int>> min_heap;
min_heap.push(18);
min_heap.push(15);
min_heap.push(12);
min_heap.push(10);
min_heap.push(19);
cout << min_heap.top() << " ";
min_heap.pop();
cout << min_heap.top() << endl;
// (6) set
cout << "SET :\n";
set<int> myset;
myset.insert(5); //inserting the elements in set
myset.insert(3);
myset.insert(8);
myset.insert(4);
myset.insert(2);
myset.insert(6);
myset.insert(9);
set<int>::iterator it = myset.find(10); // declaration of iterator for set
if (it == myset.end())
{
cout << "10 is not present in myset\n";
}
myset.erase(8); // for erasing an element
cout << myset.size() << '\n'; //size of set
// (7) Multiset
/*
We can insert multiple elements in multiset but the same can't be done with set.
*/
cout << "MULTISET: \n";
multiset<int> mymultiset;
mymultiset.insert(4);
mymultiset.insert(3);
mymultiset.insert(3);
mymultiset.insert(4);
mymultiset.insert(5);
multiset<int>::iterator it7;
it7 = mymultiset.find(4);
if (it7 != mymultiset.end())
{
cout << "4 found in mymultiset\n";
}
mymultiset.erase(it7);
cout << mymultiset.size() << "\n";
// (8) map
cout<<"MAPS : \n";
map<int,int> mp;
for(int i=0;i<5;i++)
{
mp[i]++;
}
for(auto it8=mp.begin();it8!=mp.end();it8++)
{
cout <<it8->first<<" "<<it8->second<<endl;
}
// or we can directly acess an element like :
cout <<mp[4]<<'\n';
// map of some undeclared value will return 0, please remember.
cout <<mp[199]<<'\n';
// (9) stacks
cout <<"STACK : \n";
stack<int> stk;
stk.push(10);
stk.push(9);
stk.push(8);
cout <<stk.top()<<endl;
stk.pop();
cout <<stk.top()<<endl;
cout <<stk.size()<<endl;
// (10) queue
cout <<"QUEUE : \n";
queue<int> q;
q.push(1);
q.push(2);
q.push(4);
q.push(1);
cout <<q.front()<<endl;
q.pop();
q.pop();
cout <<q.front()<<endl;
// (11) deque
cout <<"DEQUE : \n";
deque<int> dq; //doubly ended queue
dq.push_back(10); //appends an element at the back of dq
cout <<dq.size()<<endl;
dq.push_front(20); //adds element at the front of dq
cout <<dq.size()<<endl;
dq.pop_front(); //removes element from the front
cout <<dq.size()<<endl;
dq.pop_back(); // removes element from back
cout <<dq.size()<<endl;
/******************************************************END*************************************************/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment