diff --git a/STL/Array/Array.cpp b/STL/Array/Array.cpp new file mode 100644 index 0000000..ec4a7e1 --- /dev/null +++ b/STL/Array/Array.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int main() +{ + // Taking inputs using array template from C++ STL.. + array arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + array 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; +} \ No newline at end of file diff --git a/STL/Deque/dequeue.cpp b/STL/Deque/dequeue.cpp new file mode 100644 index 0000000..fcd0892 --- /dev/null +++ b/STL/Deque/dequeue.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +//Dequeue - stack + queue +int main(int argc, char const *argv[]) { + deque 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; +} diff --git a/STL/Heap/heap.cpp b/STL/Heap/heap.cpp new file mode 100644 index 0000000..12aa350 --- /dev/null +++ b/STL/Heap/heap.cpp @@ -0,0 +1,28 @@ +#include +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 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; +} diff --git a/STL/MAP/map.cpp b/STL/MAP/map.cpp new file mode 100644 index 0000000..923c225 --- /dev/null +++ b/STL/MAP/map.cpp @@ -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 +using namespace std; +int main(int argc, char const *argv[]) { + + //create a map + map 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 ::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. + +*/ diff --git a/STL/Priority Queue/priority_queue.cpp b/STL/Priority Queue/priority_queue.cpp new file mode 100644 index 0000000..d52049e --- /dev/null +++ b/STL/Priority Queue/priority_queue.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +int main(int argc, char const *argv[]) { + //make a prioity_queue + p_queue 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, greater> pq; + */ + return 0; +} diff --git a/STL/Queue/Queue.cpp b/STL/Queue/Queue.cpp new file mode 100644 index 0000000..780393a --- /dev/null +++ b/STL/Queue/Queue.cpp @@ -0,0 +1,45 @@ +/*Functions to demonstrate queue in STL +Operations: empty(), size(), front(), back(), push(g), pop()*/ + +#include +using namespace std; +//QUEUE is a FIFO data structure, where insertion and deletion of element is done at opposite ends. + +void print(queue q){ + while(!q.empty()){ + cout << q.front(); + q.pop(); + } +} + +int main(int argc, char const *argv[]) { + + //declaration of queue + queue 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 it = q.end(); + + //print queue, you can't use a normal for loop and iterator methord to print the queue. + print(q); + return 0; +} diff --git a/STL/Set/set.cpp b/STL/Set/set.cpp new file mode 100644 index 0000000..2db7a8a --- /dev/null +++ b/STL/Set/set.cpp @@ -0,0 +1,71 @@ +#include +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 > s1; + + s1.insert(40); + s1.insert(30); + s1.insert(60); + s1.insert(20); + s1.insert(50); + s1.insert(50); + s1.insert(10); + + set > :: iterator itr; + cout << "\nThe set s1 is : "; + for (itr = s1.begin(); itr != s1.end(); ++itr) + { + cout << '\t' << *itr; + } + cout << endl; + + set 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. +*/ diff --git a/STL/Stack/stack.cpp b/STL/Stack/stack.cpp new file mode 100644 index 0000000..6a35a10 --- /dev/null +++ b/STL/Stack/stack.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +//Stack follows FILO +int main(int argc, char const *argv[]) { + //make a stack. + stack 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; +} diff --git a/STL/Strings/strings.cpp b/STL/Strings/strings.cpp new file mode 100644 index 0000000..1e4199c --- /dev/null +++ b/STL/Strings/strings.cpp @@ -0,0 +1,45 @@ +#include +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; +} diff --git a/STL/Vector/Vector.cpp b/STL/Vector/Vector.cpp new file mode 100644 index 0000000..ec12a78 --- /dev/null +++ b/STL/Vector/Vector.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; +//Vector are dyanmic arrays, internally they are normal C style arrays. +int main() { + + // initilise empty vector + vector empty_vec; // {} ,empty vector + + //make a vector with default values + vector empty_vec = {12,23,45}; // {12,23,45} + + //make a vector with values from vector or array + int arr[] = {1,2,3,4,5}; + vector v1(arr,arr+n); //make a vector with array values + vector v2(v1.begin(), v1.end()); //make a new vector with old vector + + vector v3(10,0); //vector of size 10 with default value 0. + + //add a value to vector + v.push_back(45); //insert value at the vector at end. + + //remove value from vector + v.pop_back(); + + //insert at a specific position. + v.insert(v.begin(), 101); //v.insert(position, value) + + //print the value at ith position. + cout << v.at(i); + + //insert at a specific position a number of times. + v.insert(v.begin(), 10, 5); //v.insert(position, number of times to insert, + //value this insert 5, 10 times at the begin of vector. + + + //copy a vector + vector v1 = {1,2,3,4,5}; + vector v1(v1.begin(), v1.end(), v2.begin()); // (start address of copy, end address of copy + //starting address of where to copy) + + //methord-2 + vector v5; + int arr[] = {1,2,3,4,5} + v5.assign(arr, arr+n); + + //methord -3 + vector v6; + v6.assign(10,0); //assign 10, 0s to vector v6. (size, value) + + //delete a value from vector. + v.erase(it); //it = iterator to that delete position. + + //remove all values. + v.clear(); + v = {}; //both methord works + + //resize an vector. + vector v = {1,2,3,4,4,4}; + v.resize(4); //now it only contain 4 items from start rest all delets. + v.resize(10);// {1,2,3,4,4,4,0,0,0,0} + + //how many items are in between 2 iterator. + auto it = v.begin(); + auto it2 = v.end(); + cout << distance(it, it2); // print number of items at vector v. + + return 0; +}