Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added STL data structure #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions STL/Array/Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
// Taking inputs using array template from C++ STL..
array<int, 10> arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
array<int, 10> arr2 = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

cout << "Before Swap :" << endl;
cout << "Elements in the first array : ";
for (int i = 0; i < 10; i++)
cout << arr1.at(i) << " "; // This method returns value in the array at the given range.
cout << "\nElements in the second array : ";
for (int i = 0; i < 10; i++)
cout << arr2.at(i) << " ";
arr1.swap(arr2); // Swapping two arrays..
cout << "\n\nAfter Swap :" << endl;
cout << "Elements in the first array : ";
for (int i = 0; i < 10; i++)
cout << arr1.at(i) << " ";
cout << "\nElements in the second array : ";
for (int i = 0; i < 10; i++)
cout << arr2.at(i) << " ";
return 0;
}
20 changes: 20 additions & 0 deletions STL/Deque/dequeue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <bits/stdc++.h>
using namespace std;
//Dequeue - stack + queue
int main(int argc, char const *argv[]) {
deque<int> dq;

//insertion
dq.push_front(1);
dq.push_back(1);
dq.insert(it,val);
dq.insert(it,freq,val);

//delete item
dq.pop_front(1);
dq.pop_back(1);
dq.erase(it); //remove item by iterator.
dq.erase(it, it1); //remove all items inbetween that iterators.

return 0;
}
28 changes: 28 additions & 0 deletions STL/Heap/heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;
//Heaps are implemenation of priority queue data structurs.
//we have 2 types of heap, Min-Heap and Max-Heap.


//C++ privide a inplace heap data structure for implemenation of heap.
int main(int argc, char const *argv[]) {
vector<int> v = {1,2,3,4,5,6};

//make a vector a heap, C++ default heap is max heap.
make_heap(v.begin(), v.end());

//remove a element from heap.
pop_heap(v.begin(), v.end());
//note the although you have delete the item from heap but it's still in vector.

//add item at heap.
push_heap(v.begin(), v.end()); //if element is in vector but not in heap, it will add that item.

//sort heap.
sort_heap(v.begin(), v.end()); //uses heap_sort..

//print the first element of heap.
cout << heapp.front();

return 0;
}
76 changes: 76 additions & 0 deletions STL/MAP/map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//This is the code for learning all the functions of map in cpp STL.


// A map is a (key,value) pair of elements.

// ordered_map is implemented internally by red-black tree.
// unordered_map is implemented by hashing.

#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {

//create a map
map<int,char> mp;

////add values to map.
mp.insert({1,'a'}); // O(log(n)) time
mp.insert({2,'b'});
mp.insert({1,'a'}); //never contains duplicates for key(s)
mp[4] = 'f';

//make an iterator for map.
map<int,char> ::iterator it = mp.begin();
auto it = mp.begin();

//find a value
mp.find(3); // returns iterator to the item, else returns mp.end().

//delete elements from map.
mp.clear(); //clears all the elements, O(n) time
auto it = mp.find('a'); //iterator to element with value 'a' , O(lg(n)) time
mp.clear(it); //deletes the element in the iterator //O(1) time
mp.clear(it1,it1+1); //delete more than 1 element at a time.

//find size of map.
mp.size(); //O(1)

//alternative to find if a element is present or not.
mp.count(key); //return 1 if present, 0 if not present.


//binary search to map.
auto it1 = lower_bound(mp.begin(), mp.end(), val); //O(log(n))
auto it2 = upper_bound(mp.begin(), mp.end(), val); //O(log(n))

//lower bound find the value " >= " the element
//Upper bound find the value " > " the element


//if lower_bound and upper_bound needed at the same time.
auto it = equal_range(mp.begin(), mp.end(), val);
it.first //will be same as lower_bound
it.second //same as upper_bound

//iterate over a map .
for(auto it = mp.begin(); it != mp.end(); it++){
cout << it->first << " " << it->second << endl;
}
return 0;
}



/*
Note -

Multimap and unordered_map have simpliar function with same syntax.

but with a difference -

Multimap allows to insert duplicate elements,
and mp.count() returns number of occurance of that value in map.

unordered_map does'nt allows duplicates but it allows insertion, deletion, find in O(1) time.

*/
30 changes: 30 additions & 0 deletions STL/Priority Queue/priority_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
//make a prioity_queue
p_queue<int> pq;

//insert element element in p_queue
pq.push(12);

//find the size of q_queue
cout << pq.size();

//check if p_queue is empty
pq.empty();

//remove element from p-queue
pq.pop();

//print top most element.
cout << pq.top();


/*
This is by-default max- prioity_queue, to make min-queue, simple multiply -1 to all the elements
or use

p_queue<int,vector<int>, greater<int>> pq;
*/
return 0;
}
45 changes: 45 additions & 0 deletions STL/Queue/Queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*Functions to demonstrate queue in STL
Operations: empty(), size(), front(), back(), push(g), pop()*/

#include<bits/stdc++.h>
using namespace std;
//QUEUE is a FIFO data structure, where insertion and deletion of element is done at opposite ends.

void print(queue<int> q){
while(!q.empty()){
cout << q.front();
q.pop();
}
}

int main(int argc, char const *argv[]) {

//declaration of queue
queue<int> q;

//add element to QUEUE
q.push(1);

//print the front item of queue.
q.front();

//print the last item of queue.
q.back();

//delete item from queue.
q.pop();

//check if queue is empty.
q.empty(); //returns 0 or 1.

//check the size of queue.
q.size();

//iterator to queue.
auto it = q.begin();
queue<int> it = q.end();

//print queue, you can't use a normal for loop and iterator methord to print the queue.
print(q);
return 0;
}
71 changes: 71 additions & 0 deletions STL/Set/set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <bits/stdc++.h>
using namespace std;
//set is a ordered pair of item, this data structure is used when you need all the
//elements to be always sorted.
int main()
{
set <int, greater <int> > s1;

s1.insert(40);
s1.insert(30);
s1.insert(60);
s1.insert(20);
s1.insert(50);
s1.insert(50);
s1.insert(10);

set <int, greater <int> > :: iterator itr;
cout << "\nThe set s1 is : ";
for (itr = s1.begin(); itr != s1.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;

set <int> s2(s1.begin(), s1.end());

cout << "\nThe set s2 after assign from s1 is : ";
for (itr = s2.begin(); itr != s2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;

cout << "\ns2 after removal of elements less than 30 : ";
s2.erase(s2.begin(), s2.find(30));
for (itr = s2.begin(); itr != s2.end(); ++itr)
{
cout << '\t' << *itr;
}

int num;
num = s2.erase (50);
cout << "\ns2.erase(50) : ";
cout << num << " removed \t" ;
for (itr = s2.begin(); itr != s2.end(); ++itr)
{
cout << '\t' << *itr;
}

cout << endl;

cout << "s1.lower_bound(40) : "
<< *s1.lower_bound(40) << endl;
cout << "s1.upper_bound(40) : "
<< *s1.upper_bound(40) << endl;

cout << "s2.lower_bound(40) : "
<< *s2.lower_bound(40) << endl;
cout << "s2.upper_bound(40) : "
<< *s2.upper_bound(40) << endl;

// find an item in set. O(logn)
auto it = s2.find(1)
//if item is not found it returns end() iterator.
return 0;
}
/*
Set is similar to multiset, unordered set except the fact that.
unorderedset is implemented using hash table, hence insert, delete, find is much faster
than set.
*/
23 changes: 23 additions & 0 deletions STL/Stack/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;
//Stack follows FILO
int main(int argc, char const *argv[]) {
//make a stack.
stack<int> s;

//add item to stack
s.push(1);

//remove item from stack.
s.pop();

//find size of stack
s.size();

//find the top most item in stack.
s.top();

//check if stack is empty
s.empty();
return 0;
}
45 changes: 45 additions & 0 deletions STL/Strings/strings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {

//declare string
string str;
char ch[10];

//getting input
cin >> str;
getline(cin, str);

//adding something to string
str += "bc";
str.puch_back("de");
str.append("fg");

//copy string to other
str2.copy(str1, length to copy, start position);

//swap of 2 strings
str1.swap(str2);

//find a pattern in a string.
str1.find("abc");

//extract substring
str1.substr(1,4);

//lexographically compare of string.
s1 > s2
s1 < s2
s1 == s2
s1 >= s2
s1 <= s2

//replace a group of sub-string
str.replace(start index, till index, "characters");

//delete from string
str.erase(it); //it is the iterator
str.erase(position);
str.erase(position, no of items);
return 0;
}
Loading