From ee93dbf3aa6172d7193c0f5a031ad4ea5d7ad26a Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Sun, 28 Jun 2020 02:02:15 +0600 Subject: [PATCH 01/97] Update vector.md --- STL/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index ae72278..df9ae8f 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -27,7 +27,7 @@ Alternatively, we can write them also in a single line using commas, like this- ``` The size of vector `v` will be as much element we insert into the vector using `push_back()` function. - `vector < Datatype > MyVector(n);` where n = size of the vector.
-Thus we can initially allocate the memory needed for our vector like this. Using this method will give us two extra benefit: +Thus we can initially allocate the memory needed for our vector like this. This method will give us two extra benefit: - This will set the values of all index to 0 automatically. - We can now access any index form 0 to size-1 inclusively by
`“MyVector[i]”`; where i = index.
But still we can change the size of the vector at any time. Besides, we can also store more than n elements into the vector (if needed). Using `push_back()`, we can add elements from index n to maximum size. From a5597d5503c81fcf2ade151a5dbbacda857f9260 Mon Sep 17 00:00:00 2001 From: Shawon Majid Date: Sun, 28 Jun 2020 02:38:02 +0600 Subject: [PATCH 02/97] Update pair.md --- STL/pair.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/STL/pair.md b/STL/pair.md index ab01505..e9bd855 100644 --- a/STL/pair.md +++ b/STL/pair.md @@ -1 +1,19 @@ # Let's Make some Pairs! + +## __introduction__ +std::pair is an another container which contains two data elements or objects. It can contain two same type of objects (i.e int, int) or different type of objects (i.e string, int). Pair provides a way to store two heterogeneous objects as a single unit. Pairs are widely used in STL. So, lets jump into it right now! + +## Header File +we use `#include ` for using pairs and related functions. +## Syntax and declaration +genaral syntax is : `pair pair name` +Now lets declare the simplest form, pair of 2 integers: +
+
+`pair < int, int > p;` +
+
+now this single unit **p** denotes a pair of two integers. **p** has 2 forms: **p.first** and **p.second** which consecutively means the first and the second element of pair **p**. In this case both are integers. +We could also use `pair p` , `pair p`, `pair > p` and so on. +## Taking input and output +Now, lets see some ways to take input of pairs: From bda26ee8ad81736d7272a8e0ff06b8af61c18d3c Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Sun, 28 Jun 2020 04:44:06 +0600 Subject: [PATCH 03/97] Update vector.md --- STL/vector.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index df9ae8f..bd40048 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -13,7 +13,7 @@ Under this header file, we can access all the library functions that can be used We can declare a vector in several ways. Let us see them one by one and understand their differences gradually. - `vector < Datatype > MyVector;`
Using this format, we can declare a vector. Now we can see that the size of the vector is not mentioned here. That means **“we have not allocated any memory for this vector.”** In this case if we want to add any value (of the datatype mentioned inside), we have to use a library function `push_back()`. For example, if we want to store 55, 72, 89 and 100 into an integer vector, we have to perform these: -``` +```cpp vector < int > v; v.push_back(55); v.push_back(72); @@ -21,7 +21,7 @@ Using this format, we can declare a vector. Now we can see that the size of the v.push_back(100); ``` Alternatively, we can write them also in a single line using commas, like this- -``` +```cpp vector < int > v; v.push_back(55), v.push_back(72), v.push_back(89), v.push_back(100); ``` @@ -33,13 +33,13 @@ Thus we can initially allocate the memory needed for our vector like this. This But still we can change the size of the vector at any time. Besides, we can also store more than n elements into the vector (if needed). Using `push_back()`, we can add elements from index n to maximum size. Suppose, we are declaring a vector of length 3 and store 3 values into the vector. we have to do it like- -``` +```cpp vector < int > v(3); v[0] = 29, v[1] = 37, v[2] = 23; ``` Now if we want to add more elements, we can do it by simply using `push_back()`.
-``` +```cpp v.push_back(18), v.push_back(97); ``` **Now the size of the vector is increased from three to five**. From 5a47fa6440fe30ff3063a131439181ecdb27b853 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 13:52:06 +0600 Subject: [PATCH 04/97] Create priority_queue.md --- STL/priority_queue.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 STL/priority_queue.md diff --git a/STL/priority_queue.md b/STL/priority_queue.md new file mode 100644 index 0000000..7cb48cf --- /dev/null +++ b/STL/priority_queue.md @@ -0,0 +1,6 @@ +# Priority Queue +Priority queue is a special variant of the queue which works like a **heap data structure**. This is very important sometimes in some situation to use this heap. As it uses heap it can store data in increasing or decreasing order. This can pull out maximum or minimum element of a list in _**`logN`**_ time where **N** is the size of the list. So You can get all the elements of the array in _**`NlogN`**_ time. + +> [**To know about HEAP click here**](https://en.wikipedia.org/wiki/Heap_(data_structure)) + +To use priority queue you need to first add its header by typing From 952e3d7e53876ec7930396ef5012f3d9773c7412 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 14:06:43 +0600 Subject: [PATCH 05/97] CREATED Priority_queue Also added some introduction --- STL/{priority_queue.md => PriorityQueue.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename STL/{priority_queue.md => PriorityQueue.md} (100%) diff --git a/STL/priority_queue.md b/STL/PriorityQueue.md similarity index 100% rename from STL/priority_queue.md rename to STL/PriorityQueue.md From 3068e75d9f6678686630361e4e2dd275cf68c5ba Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 14:16:42 +0600 Subject: [PATCH 06/97] Update priority queue Added header and Declaration --- STL/PriorityQueue.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 7cb48cf..4d939d7 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -1,6 +1,19 @@ # Priority Queue +## Introduction Priority queue is a special variant of the queue which works like a **heap data structure**. This is very important sometimes in some situation to use this heap. As it uses heap it can store data in increasing or decreasing order. This can pull out maximum or minimum element of a list in _**`logN`**_ time where **N** is the size of the list. So You can get all the elements of the array in _**`NlogN`**_ time. > [**To know about HEAP click here**](https://en.wikipedia.org/wiki/Heap_(data_structure)) -To use priority queue you need to first add its header by typing +## Header File +To use priority queue you need to first add its header by typing the following : +```cpp + #include +``` +By default priority queue stores data in **decreasing order** which allows you to _pop the maximum value_ of the list in each operation of _**logN**_ time. + +## Declaration +You can declare a priority queue using following code : +```cpp +priority_queue < data_type > MyPriorityQueue; +``` +This creates a priority queue nammed MyPriorityQueue of a cerrain data type. From 052764c1b3fc416a17703fc02c3c09d1240d9ca7 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 14:28:56 +0600 Subject: [PATCH 07/97] update priority_queue Added some oparation : push pop front --- STL/PriorityQueue.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 4d939d7..cd1c6cd 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -17,3 +17,39 @@ You can declare a priority queue using following code : priority_queue < data_type > MyPriorityQueue; ``` This creates a priority queue nammed MyPriorityQueue of a cerrain data type. + +## Operations +You can do the following operation in a priority queue : +- push +- pop +- front + + _**PUSH :**_ This operation takes an input of certain data type and pushes it into your priority queue or heap. It works same as pushing something in a heap. It take logN time to do this operation. Suppose you want to push 12, 18, 8 into your priority queue. You can do that using the following code: + ```cpp + priority_queue < int > MyPQ; + MyPQ.push(12); + MyPQ.push(18); + MyPQ.push(8); + ``` + This code makes a priority queue of integers named MyPQ and pushes 12, 18 and 8 into that. Then it works like a heap and so you can pop out the maximum number from the heap list in just logN time which means this 3 numbers will be stored in this sequence { 18, 12, 8 }. + + _**POP :**_ You can retrieve the maximum number from the numbers you pushed earlier using this operation. You can see the following code to see the implementation of this operation : + ```cpp + priority_queue < int > MyPQ; + MyPQ.push(12); + MyPQ.push(18); + MyPQ.push(8); + // After Pushing the queue will be 18, 12, 8 + MyPQ.pop(); // popping 18 + MyPQ.pop(); // popping 12 + MyPQ.pop(); // popping 8 + // Now the queue will be empty + ``` + This pop functionality works like popping from a heap. And after popping it automatically adjusts the queue in logN time. + + _**FRONT :**_ This function gives us the access to the topmost element of the priority queue. If the queue is in default mode it will return the maximum element and if it is declared as the smaller version of the heap it will return the smallest number of the list. See the following code for better understanding : + ```cpp + priority_queue < int > MyPQ; + MyPQ.push(12); + MyPQ.push(18); + MyPQ.push(8); + cout << MyPQ.front() << endl; + // prints 18 as it is the largest among the heap + ``` From ad0951b6305138605301e0df76636822cf33e370 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 14:32:24 +0600 Subject: [PATCH 08/97] create set.md added introduction --- STL/set.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 STL/set.md diff --git a/STL/set.md b/STL/set.md new file mode 100644 index 0000000..74bedc3 --- /dev/null +++ b/STL/set.md @@ -0,0 +1,7 @@ +# Set + +## Introduction +Set is a container which has very special for its functionality. This have some variants which are also discussed here later on. The main features of default set is : +- It stores each element once and don't allow repeated elements. Which means default set stores only distinct elements. +- It keeps elements in sorted order. You can customize how the elements will be sorted. But By default it keeps the elements in ascending order. +- You can use other variants of the set like multiset, unordered_set, unordered_multiset as you need. From 77b1cde8e2f2f8b10cfcea3c4fc31a31dfffa7c7 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 14:36:27 +0600 Subject: [PATCH 09/97] update set.md Added declarion --- STL/set.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/STL/set.md b/STL/set.md index 74bedc3..14780cc 100644 --- a/STL/set.md +++ b/STL/set.md @@ -5,3 +5,13 @@ Set is a container which has very special for its functionality. This have some - It stores each element once and don't allow repeated elements. Which means default set stores only distinct elements. - It keeps elements in sorted order. You can customize how the elements will be sorted. But By default it keeps the elements in ascending order. - You can use other variants of the set like multiset, unordered_set, unordered_multiset as you need. + +## Declaration: +First of all you need to add the header file of set to use any functionality of set. To do that you have to add the following header : +```cpp +#include +``` +After adding the header you can declare your set using this code: +```cpp +set < data_type > MySet; +``` From 56a49501ab15e19db667af9fc9f983f80fffadb5 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 14:49:06 +0600 Subject: [PATCH 10/97] Update home.md --- STL/home.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/STL/home.md b/STL/home.md index beb1c8f..7909d76 100644 --- a/STL/home.md +++ b/STL/home.md @@ -11,8 +11,8 @@ STL is a very powefull tool for **competitive programming**. It saves a lot of t - [Stack](https://definecoder.github.io/STL/stack) - Queue * Deque -- Priority Queue -- Set +- [Priority Queue](https://definecoder.github.io/STL/PriorityQueue) +- [Set](https://definecoder.github.io/STL/set) * Multiset * Unordered Set * Unordered Multiset From 9cf53c4b6dcb287cbc8f4fd2d0f3ef72e51d003e Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 15:51:07 +0600 Subject: [PATCH 11/97] Corrected a mistake swaped top with front --- STL/PriorityQueue.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index cd1c6cd..d129fdd 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -22,7 +22,7 @@ This creates a priority queue nammed MyPriorityQueue of a cerrain data type. You can do the following operation in a priority queue : - push - pop -- front +- top + _**PUSH :**_ This operation takes an input of certain data type and pushes it into your priority queue or heap. It works same as pushing something in a heap. It take logN time to do this operation. Suppose you want to push 12, 18, 8 into your priority queue. You can do that using the following code: ```cpp priority_queue < int > MyPQ; @@ -44,12 +44,12 @@ You can do the following operation in a priority queue : // Now the queue will be empty ``` This pop functionality works like popping from a heap. And after popping it automatically adjusts the queue in logN time. - + _**FRONT :**_ This function gives us the access to the topmost element of the priority queue. If the queue is in default mode it will return the maximum element and if it is declared as the smaller version of the heap it will return the smallest number of the list. See the following code for better understanding : + + _**TOP :**_ This function gives us the access to the topmost element of the priority queue. If the queue is in default mode it will return the maximum element and if it is declared as the smaller version of the heap it will return the smallest number of the list. See the following code for better understanding : ```cpp priority_queue < int > MyPQ; MyPQ.push(12); MyPQ.push(18); MyPQ.push(8); - cout << MyPQ.front() << endl; + cout << MyPQ.top() << endl; // prints 18 as it is the largest among the heap ``` From b9e7f3dad5255da544c151093b62bb008322c6e8 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sun, 28 Jun 2020 20:32:37 +0600 Subject: [PATCH 12/97] Update PriorityQueue.md --- STL/PriorityQueue.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index d129fdd..8ba26b6 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -2,7 +2,7 @@ ## Introduction Priority queue is a special variant of the queue which works like a **heap data structure**. This is very important sometimes in some situation to use this heap. As it uses heap it can store data in increasing or decreasing order. This can pull out maximum or minimum element of a list in _**`logN`**_ time where **N** is the size of the list. So You can get all the elements of the array in _**`NlogN`**_ time. -> [**To know about HEAP click here**](https://en.wikipedia.org/wiki/Heap_(data_structure)) +> [_**To know about HEAP click here**_](https://en.wikipedia.org/wiki/Heap_(data_structure)) ## Header File To use priority queue you need to first add its header by typing the following : From ebef99354ba3f5c83ebac4e6d588fdfd3393a9c1 Mon Sep 17 00:00:00 2001 From: Samiul Islam Mugdha <67468157+mugdha-samiul@users.noreply.github.com> Date: Sun, 28 Jun 2020 20:36:26 +0600 Subject: [PATCH 13/97] Update stack.md --- STL/stack.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/STL/stack.md b/STL/stack.md index 306554a..9851303 100644 --- a/STL/stack.md +++ b/STL/stack.md @@ -1,2 +1,14 @@ # stack -tutorial about stack + +## Introduction: +Stack is a type of container adaptor. It is just like a pile of plates kept on top of each other. we can do 2 things with a pile of plates: +- put a new plate on top +- remove the top plate +Such an arrangement is called Last In First Out(LIFO) which is not possible with Linked list and array. Stack is specifically designed to operate in a LIFO context, where elements are inserted and extracted only from one end of the container. + +## Header File: +we can use `#include ` to access all the library functions of stack. + +## Declaration: +Genaral syntax to declare a stack is : `stack < datatype > MyStack;`. +we can use any kind of datatype, stracture, container in a stack. example : `stack < int > MyStack`, `stack < char > MyStack`, `stack < vector < string > > MyStack`. From 702199b859949152a3bac1becd0f268378b3fe95 Mon Sep 17 00:00:00 2001 From: Samiul Islam Mugdha <67468157+mugdha-samiul@users.noreply.github.com> Date: Sun, 28 Jun 2020 20:43:27 +0600 Subject: [PATCH 14/97] added syntex highlighting added syntex highlighting and changed titke --- STL/stack.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/STL/stack.md b/STL/stack.md index 9851303..3aa4405 100644 --- a/STL/stack.md +++ b/STL/stack.md @@ -1,4 +1,4 @@ -# stack +# STACK ## Introduction: Stack is a type of container adaptor. It is just like a pile of plates kept on top of each other. we can do 2 things with a pile of plates: @@ -11,4 +11,10 @@ we can use `#include ` to access all the library functions of stack. ## Declaration: Genaral syntax to declare a stack is : `stack < datatype > MyStack;`. -we can use any kind of datatype, stracture, container in a stack. example : `stack < int > MyStack`, `stack < char > MyStack`, `stack < vector < string > > MyStack`. +we can use any kind of datatype, stracture, container in a stack. Example : +```cpp +stack < int > MyStack; +stack < char > MyStack; +stack < vector < string > > MyStack; +``` + From 7a2580cc2916a603ac93fda920105cd39db7d29a Mon Sep 17 00:00:00 2001 From: Shawon Majid Date: Mon, 29 Jun 2020 00:10:34 +0600 Subject: [PATCH 15/97] Finished pair --- STL/pair.md | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/STL/pair.md b/STL/pair.md index e9bd855..944d690 100644 --- a/STL/pair.md +++ b/STL/pair.md @@ -16,4 +16,73 @@ Now lets declare the simplest form, pair of 2 integers: now this single unit **p** denotes a pair of two integers. **p** has 2 forms: **p.first** and **p.second** which consecutively means the first and the second element of pair **p**. In this case both are integers. We could also use `pair p` , `pair p`, `pair > p` and so on. ## Taking input and output -Now, lets see some ways to take input of pairs: +Enough of talking now it's time to code! Let's jump into it: +```cpp +#include +#include +using namespace std; +int main() +{ + pair p; + p.first = 23; + p.second = 12; + cout << p.first << ' ' << p.second << endl; + return 0; +} +``` +We could also take input from user: +```cpp +cin >> p.first >> p.second; +``` +There is another way to take input of pair let's see this: +```cpp +p.make_pair(x, y); +``` +In this case **x** and **y** are any two variable. + +## Use of pairs + +There are many usage of pair. For instance, using vector of pairs can be very convenient sometime. To understand this, lets consider a scenario: + +`You have to take input n people's names and their IDs. Then you have to store all the data in a vector of pairs. Then print it in reverse order.` +``` +input: +3 +Shawon 1013 +Mehraj 1074 +Shanto 1029 +output: +Shanto 1029 +Mehraj 1074 +Shawon 1013 +``` +**You must Try to code this by yourself first and then proceed to the next portion!** +
+Now lets see how we can code with the help of pairs! +```cpp +#include +#include +#include +using namespace std; + +int main() +{ + int n; + cin >> n; + vector < pair > vp(n); + + for(int i = 0; i < n; i++){ + cin >> vp[i].first >> vp[i].second; + } + + for(int i = n-1; i >= 0; i--){ + cout << vp[i].first << ' ' << vp[i].second << endl; + } + return 0; +} +``` +**We could also sort the vectors of pairs in the increasing order of id or or name by using customized sort functions, which will be shown in the sorting part of STL** + +Now that you can make pairs so start solving some problems using pairs! + + From 024d0abaf285d3baee7291c89f1fa601e6cd0009 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 00:37:07 +0600 Subject: [PATCH 16/97] changed format changed format --- STL/PriorityQueue.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 8ba26b6..ccd4ab5 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -23,7 +23,8 @@ You can do the following operation in a priority queue : - push - pop - top - + _**PUSH :**_ This operation takes an input of certain data type and pushes it into your priority queue or heap. It works same as pushing something in a heap. It take logN time to do this operation. Suppose you want to push 12, 18, 8 into your priority queue. You can do that using the following code: +*** ++ _**PUSH :**_ This operation takes an input of certain data type and pushes it into your priority queue or heap. It works same as pushing something in a heap. It take logN time to do this operation. Suppose you want to push 12, 18, 8 into your priority queue. You can do that using the following code: ```cpp priority_queue < int > MyPQ; MyPQ.push(12); @@ -31,7 +32,7 @@ You can do the following operation in a priority queue : MyPQ.push(8); ``` This code makes a priority queue of integers named MyPQ and pushes 12, 18 and 8 into that. Then it works like a heap and so you can pop out the maximum number from the heap list in just logN time which means this 3 numbers will be stored in this sequence { 18, 12, 8 }. - + _**POP :**_ You can retrieve the maximum number from the numbers you pushed earlier using this operation. You can see the following code to see the implementation of this operation : ++ _**POP :**_ You can retrieve the maximum number from the numbers you pushed earlier using this operation. You can see the following code to see the implementation of this operation : ```cpp priority_queue < int > MyPQ; MyPQ.push(12); @@ -44,7 +45,7 @@ You can do the following operation in a priority queue : // Now the queue will be empty ``` This pop functionality works like popping from a heap. And after popping it automatically adjusts the queue in logN time. - + _**TOP :**_ This function gives us the access to the topmost element of the priority queue. If the queue is in default mode it will return the maximum element and if it is declared as the smaller version of the heap it will return the smallest number of the list. See the following code for better understanding : ++ _**TOP :**_ This function gives us the access to the topmost element of the priority queue. If the queue is in default mode it will return the maximum element and if it is declared as the smaller version of the heap it will return the smallest number of the list. See the following code for better understanding : ```cpp priority_queue < int > MyPQ; MyPQ.push(12); @@ -53,3 +54,4 @@ You can do the following operation in a priority queue : cout << MyPQ.top() << endl; // prints 18 as it is the largest among the heap ``` +*** From 2b6d0beff57e46461af94ccbac12730a31811c7c Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 02:21:25 +0600 Subject: [PATCH 17/97] ADDED MINIMUM HEAP in Priority queue - ADDED MINIMUM HEAP in the Priority queue - Added Printing minimum heap PQ function too --- STL/PriorityQueue.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index ccd4ab5..b517633 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -55,3 +55,46 @@ You can do the following operation in a priority queue : // prints 18 as it is the largest among the heap ``` *** +## Printing a Priority Queue using a custom function : + +## Set the Minimum Number as priority ( Minimum Number Heap ) +Sometimes your priority can be to pop the smallest number first. Which means if you want to keep your data in ascending order you need to declare the priority queue using the following code : +```cpp +#include +#include + +using namespace std; + +// Function to print minimum heap priority queue +void print_spcl_priority_queue(priority_queue < int , vector < int > , greater < int > > MyPQ) +{ + priority_queue < int , vector < int > , greater < int > > temp = MyPQ; + cout << "Elements of the priority_queue are :: \n" ; + while ( !temp.empty() ) + { + cout << temp.top() << " "; + temp.pop(); + } + cout << '\n'; +} + +int main(){ + priority_queue < int , vector < int > , greater < int > > MySpclPQ; + MySpclPQ.push(12); + MySpclPQ.push(18); + MySpclPQ.push(8); + // After Pushing the queue will be 8 , 12 , 18 + print_spcl_priority_queue(MySpclPQ); + // printing the priority queue + MySpclPQ.pop(); // popping 8 + MySpclPQ.pop(); // popping 12 + MySpclPQ.pop(); // popping 18 + // Now the queue will be empty + return 0; +} +``` +### OUTPUT : +``` +Elements of the priority_queue are :: +8 12 18 +``` From 3f72adcd1d8850994bc285cc6e084fbc8c9a0468 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 02:35:39 +0600 Subject: [PATCH 18/97] ADDED and UPDATED priority_queue.md Added Printing function and updated some other lines in minimum heap --- STL/PriorityQueue.md | 45 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index b517633..b6888ec 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -56,6 +56,45 @@ You can do the following operation in a priority queue : ``` *** ## Printing a Priority Queue using a custom function : +There are no library function to print a queue without popping. So, we will write a custom function of oue own to print a priority queue. We will send a copy of our priority queue in order to print it. We can pop that copy because it will not change the main Priority_queue which is inside the main function. So, Lets see the code : +```cpp +#include +#include + +using namespace std; + +void print_priority_queue(priority_queue < int > temp) +{ + cout << "Elements of the priority_queue are :: \n" ; + while ( !temp.empty() ) + { + cout << temp.top() << " "; + temp.pop(); + } + cout << '\n'; +} + +int main(){ + priority_queue < int > MyPQ; + MyPQ.push(12); + MyPQ.push(18); + MyPQ.push(8); + // After Pushing the queue will be 18, 12, 8 + print_priority_queue(MyPQ); + // printing the priority queue using custom function + // Here we are sending a copy of the queue + MyPQ.pop(); // popping 18 + MyPQ.pop(); // popping 12 + MyPQ.pop(); // popping 8 + // Now the queue will be empty + return 0; +} +``` +### OUTPUT : +``` +Elements of the priority_queue are :: +18 12 8 +``` ## Set the Minimum Number as priority ( Minimum Number Heap ) Sometimes your priority can be to pop the smallest number first. Which means if you want to keep your data in ascending order you need to declare the priority queue using the following code : @@ -66,9 +105,8 @@ Sometimes your priority can be to pop the smallest number first. Which means if using namespace std; // Function to print minimum heap priority queue -void print_spcl_priority_queue(priority_queue < int , vector < int > , greater < int > > MyPQ) +void print_spcl_priority_queue(priority_queue < int , vector < int > , greater < int > > temp) { - priority_queue < int , vector < int > , greater < int > > temp = MyPQ; cout << "Elements of the priority_queue are :: \n" ; while ( !temp.empty() ) { @@ -95,6 +133,7 @@ int main(){ ``` ### OUTPUT : ``` -Elements of the priority_queue are :: +Elements of the minimum_heap_priority_queue are :: 8 12 18 ``` +Here we changed the argument of the queue printing function to _**`priority_queue < int , vector < int > , greater < int > > temp`**_ To let the function know that it is a minimum heap. From 328131b1ed21ceebff2b969e154a9a43f4033726 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 02:45:12 +0600 Subject: [PATCH 19/97] Update PriorityQueue.md --- STL/PriorityQueue.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index b6888ec..2b25902 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -137,3 +137,12 @@ Elements of the minimum_heap_priority_queue are :: 8 12 18 ``` Here we changed the argument of the queue printing function to _**`priority_queue < int , vector < int > , greater < int > > temp`**_ To let the function know that it is a minimum heap. + +## Functions : +| Function | Work of the function | +|:-------------:|:------------------------------------------------------------:| +| MyPQ.empty() | Returns True is MyPQ is empty otherwise returns False | +| MyPQ.top() | Returns the most prioritized (Topmost) element of the queue | +| MyPQ.pop() | Removes the most prioritized (Topmost) element of the queue | +| MyPQ.push(x) | Pushes x into the heap | +| pq1.swap(pq2) | Swaps the values from pq1 to pq2 | From fefea33c73be72998a0350fa560c597b1e7a49aa Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 02:53:29 +0600 Subject: [PATCH 20/97] Fixed syntax Fixed some issue in showing in website --- STL/PriorityQueue.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 2b25902..6d92953 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -56,7 +56,8 @@ You can do the following operation in a priority queue : ``` *** ## Printing a Priority Queue using a custom function : -There are no library function to print a queue without popping. So, we will write a custom function of oue own to print a priority queue. We will send a copy of our priority queue in order to print it. We can pop that copy because it will not change the main Priority_queue which is inside the main function. So, Lets see the code : +There are no library function to print a queue without popping. So, we will write a custom function of oue own to print a priority queue. We will send a copy of our priority queue in order to print it. We can pop that copy because it will not change the main Priority_queue which is inside the main function. So, Lets see the code : + ```cpp #include #include @@ -89,15 +90,15 @@ int main(){ // Now the queue will be empty return 0; } -``` -### OUTPUT : +``` +### OUTPUT : ``` Elements of the priority_queue are :: 18 12 8 ``` ## Set the Minimum Number as priority ( Minimum Number Heap ) -Sometimes your priority can be to pop the smallest number first. Which means if you want to keep your data in ascending order you need to declare the priority queue using the following code : +Sometimes your priority can be to pop the smallest number first. Which means if you want to keep your data in ascending order you need to declare the priority queue using the following code : ```cpp #include #include @@ -130,15 +131,16 @@ int main(){ // Now the queue will be empty return 0; } -``` -### OUTPUT : +``` +### OUTPUT : ``` Elements of the minimum_heap_priority_queue are :: 8 12 18 ``` Here we changed the argument of the queue printing function to _**`priority_queue < int , vector < int > , greater < int > > temp`**_ To let the function know that it is a minimum heap. -## Functions : +## Functions : + | Function | Work of the function | |:-------------:|:------------------------------------------------------------:| | MyPQ.empty() | Returns True is MyPQ is empty otherwise returns False | From 478c167dd76231aec2cfe6ebcc4833b3f7da4915 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 02:56:32 +0600 Subject: [PATCH 21/97] Update PriorityQueue.md --- STL/PriorityQueue.md | 141 +++++++++++++++++++++---------------------- 1 file changed, 70 insertions(+), 71 deletions(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 6d92953..6b80e30 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -57,86 +57,85 @@ You can do the following operation in a priority queue : *** ## Printing a Priority Queue using a custom function : There are no library function to print a queue without popping. So, we will write a custom function of oue own to print a priority queue. We will send a copy of our priority queue in order to print it. We can pop that copy because it will not change the main Priority_queue which is inside the main function. So, Lets see the code : + ```cpp + #include + #include -```cpp -#include -#include - -using namespace std; + using namespace std; -void print_priority_queue(priority_queue < int > temp) -{ - cout << "Elements of the priority_queue are :: \n" ; - while ( !temp.empty() ) - { - cout << temp.top() << " "; - temp.pop(); - } - cout << '\n'; -} + void print_priority_queue(priority_queue < int > temp) + { + cout << "Elements of the priority_queue are :: \n" ; + while ( !temp.empty() ) + { + cout << temp.top() << " "; + temp.pop(); + } + cout << '\n'; + } -int main(){ - priority_queue < int > MyPQ; - MyPQ.push(12); - MyPQ.push(18); - MyPQ.push(8); - // After Pushing the queue will be 18, 12, 8 - print_priority_queue(MyPQ); - // printing the priority queue using custom function - // Here we are sending a copy of the queue - MyPQ.pop(); // popping 18 - MyPQ.pop(); // popping 12 - MyPQ.pop(); // popping 8 - // Now the queue will be empty - return 0; -} -``` -### OUTPUT : -``` -Elements of the priority_queue are :: -18 12 8 -``` + int main(){ + priority_queue < int > MyPQ; + MyPQ.push(12); + MyPQ.push(18); + MyPQ.push(8); + // After Pushing the queue will be 18, 12, 8 + print_priority_queue(MyPQ); + // printing the priority queue using custom function + // Here we are sending a copy of the queue + MyPQ.pop(); // popping 18 + MyPQ.pop(); // popping 12 + MyPQ.pop(); // popping 8 + // Now the queue will be empty + return 0; + } + ``` +### OUTPUT: + ``` + Elements of the priority_queue are :: + 18 12 8 + ``` ## Set the Minimum Number as priority ( Minimum Number Heap ) Sometimes your priority can be to pop the smallest number first. Which means if you want to keep your data in ascending order you need to declare the priority queue using the following code : -```cpp -#include -#include - -using namespace std; + ```cpp + #include + #include -// Function to print minimum heap priority queue -void print_spcl_priority_queue(priority_queue < int , vector < int > , greater < int > > temp) -{ - cout << "Elements of the priority_queue are :: \n" ; - while ( !temp.empty() ) - { - cout << temp.top() << " "; - temp.pop(); - } - cout << '\n'; -} + using namespace std; -int main(){ - priority_queue < int , vector < int > , greater < int > > MySpclPQ; - MySpclPQ.push(12); - MySpclPQ.push(18); - MySpclPQ.push(8); - // After Pushing the queue will be 8 , 12 , 18 - print_spcl_priority_queue(MySpclPQ); - // printing the priority queue - MySpclPQ.pop(); // popping 8 - MySpclPQ.pop(); // popping 12 - MySpclPQ.pop(); // popping 18 - // Now the queue will be empty - return 0; -} -``` + // Function to print minimum heap priority queue + void print_spcl_priority_queue(priority_queue < int , vector < int > , greater < int > > temp) + { + cout << "Elements of the priority_queue are :: \n" ; + while ( !temp.empty() ) + { + cout << temp.top() << " "; + temp.pop(); + } + cout << '\n'; + } + + int main(){ + priority_queue < int , vector < int > , greater < int > > MySpclPQ; + MySpclPQ.push(12); + MySpclPQ.push(18); + MySpclPQ.push(8); + // After Pushing the queue will be 8 , 12 , 18 + print_spcl_priority_queue(MySpclPQ); + // printing the priority queue + MySpclPQ.pop(); // popping 8 + MySpclPQ.pop(); // popping 12 + MySpclPQ.pop(); // popping 18 + // Now the queue will be empty + return 0; + } + ``` ### OUTPUT : -``` -Elements of the minimum_heap_priority_queue are :: -8 12 18 -``` + ``` + Elements of the minimum_heap_priority_queue are :: + 8 12 18 + ``` Here we changed the argument of the queue printing function to _**`priority_queue < int , vector < int > , greater < int > > temp`**_ To let the function know that it is a minimum heap. ## Functions : From 74cb54726afade7a27ac0c67158741b371d7e9ce Mon Sep 17 00:00:00 2001 From: Samiul Islam Mugdha <67468157+mugdha-samiul@users.noreply.github.com> Date: Mon, 29 Jun 2020 06:16:15 +0600 Subject: [PATCH 22/97] added functions + table i have added all the functions ans description, also added a table. some problem links are also given below --- STL/stack.md | 115 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 10 deletions(-) diff --git a/STL/stack.md b/STL/stack.md index 3aa4405..31f73d6 100644 --- a/STL/stack.md +++ b/STL/stack.md @@ -1,20 +1,115 @@ -# STACK +# **STACK** -## Introduction: +## **Introduction:** Stack is a type of container adaptor. It is just like a pile of plates kept on top of each other. we can do 2 things with a pile of plates: -- put a new plate on top -- remove the top plate -Such an arrangement is called Last In First Out(LIFO) which is not possible with Linked list and array. Stack is specifically designed to operate in a LIFO context, where elements are inserted and extracted only from one end of the container. + - put a new plate on top + - remove the top plate + +Such an arrangement is called Last In First Out(LIFO) which is not possible with Linked list and array. Stack is specifically designed to operate in a LIFO context, where a new element is added at one end called the 'top' of the stack and an element is removed from the same end only. -## Header File: +## **Header File:** we can use `#include ` to access all the library functions of stack. -## Declaration: +## **Declaration:** Genaral syntax to declare a stack is : `stack < datatype > MyStack;`. we can use any kind of datatype, stracture, container in a stack. Example : ```cpp -stack < int > MyStack; -stack < char > MyStack; -stack < vector < string > > MyStack; + stack < int > MyStack; + stack < char > MyStack; + stack < vector < string > > MyStack; ``` +## **functions:** +There are mainly four functions which are hugely used in stack. They are: + - push() + - pop() + - top() + - empty() +##### push(): +push() function is used to input a data in the stack. This function always adds a new data on the top of the container and increases the size of the stack by one. Example: +```cpp + stack < int > MyStack; + MyStack.push(5); + MyStack.push(15); + MyStack.push(63); +``` +In this code, we have inputed some data in "MyStack". After pushing some datas the stack will be {5, 15, 63}. As '63' is the last data we have inserted, so it is on the top of the stack. + +##### pop(): +pop() is the opposite of the push() function. It removes the last data inserted in the stack. It decreases the size of the stack by one.The items are popped in the reversed order in which they are pushed. +```cpp + stack < int > MyStack; + MyStack.push(5); + MyStack.push(15); + MyStack.push(63); + //we have pushed 5, 15, 63 in the stack, the stack is{5, 15, 63} now + MyStack.pop(); //popping 63 from the stack, the stack is {5, 15} now + MyStack.pop(); //popping 15 from the stack, the stack is {5} now + MyStack.pop(); //popping 5 from the stack, the stack is empty now +``` +pop() function always deletes the data in the reversed order it was pushed. As we pushed 63 at the last it was popped at first. + +#### top(): +top() function always returns a reference to the top element(the newest) of the stack. Let's see the code: +```cpp + stack < int > MyStack; + MyStack.push(5); + MyStack.push(15); + MyStack.push(63); + cout << MyStack.top() << "\n"; +``` +`OUTPUT : 63` + +In this code '63' is on the top of the stack, so top() function will return '63'. + +#### empty(): +empty() function is used to check if the stack container is empty or not. If the stack is **empty** it returns **TRUE**. Otherwise, if the container has **at least one element** it returns **FALSE**. Let's see the example: +```cpp + stack < int > MyStack; + MyStack.push(5); //pushing an element in the stack + if(MyStack.empty() != 1) + cout << "EMPTY\n"; //if empty() funtion returns false it'll print "EMPTY" + else + cout << "FILLED\d" //if empty() funtion returns true it'll print "FILLED" +``` +`OUTPUT : FILLED` + +In this code we gave a input in MyStack, so the empty() function returned 1. As a result we got "FILLED" as an output. + +Let's see another code: +```cpp + stack < int > MyStack; + MyStack.push(5); + MyStack.push(15); + MyStack.push(63); + //the stack is {5, 15, 63} now + while(MyStack.empty() != 1){ //the loop runs untill empty() function returns 1 + cout << MyStack.top() <<" "; //prints the last element of the stack + MyStack.pop(); //decreasing the size of the stack by removing the last element + } +``` +`OUTPUT : 5 15 63` + +In this code we have firstly inputed 5, 15, 63. Then we have run a while loop. If empty() function returns 0, the loop will break. In the loop we have printed the top element everytime and also removed it from the stack. As a result the loop will run for 3 times. On the third time the last element 5 will be removed ans our stack will become empty. We know that when a stack becomes empty the empty() function will return FALSE. So the loop breaks. + +Let's see all the functions used in stack at a glance: + +|function |description| +|:-- |:-- | +|push |Insert element| +|pop |Remove top element| +|top |Access next element| +|empty |Test whether container is empty| +|size |Access next element| +|swap |Swap contents| + + +Time complexity of all above functions is O(1). + +So these were the basic concepts of stack. We must need to solve stack related problems. **Some problem links are given below:** + +- [*UVA 514*](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455) +- [*UVA 1062*](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3503) +- [*Codeforces 5C*](https://codeforces.com/contest/5/status) + +Happy coding<3 From ca13de822989c9cb77ee1625abcd33d414902545 Mon Sep 17 00:00:00 2001 From: Samiul Islam Mugdha <67468157+mugdha-samiul@users.noreply.github.com> Date: Mon, 29 Jun 2020 06:30:22 +0600 Subject: [PATCH 23/97] fixed links --- STL/stack.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/STL/stack.md b/STL/stack.md index 31f73d6..f9afb64 100644 --- a/STL/stack.md +++ b/STL/stack.md @@ -25,7 +25,8 @@ There are mainly four functions which are hugely used in stack. They are: - pop() - top() - empty() -##### push(): + +#### push(): push() function is used to input a data in the stack. This function always adds a new data on the top of the container and increases the size of the stack by one. Example: ```cpp stack < int > MyStack; @@ -35,7 +36,7 @@ push() function is used to input a data in the stack. This function always adds ``` In this code, we have inputed some data in "MyStack". After pushing some datas the stack will be {5, 15, 63}. As '63' is the last data we have inserted, so it is on the top of the stack. -##### pop(): +#### pop(): pop() is the opposite of the push() function. It removes the last data inserted in the stack. It decreases the size of the stack by one.The items are popped in the reversed order in which they are pushed. ```cpp stack < int > MyStack; @@ -74,7 +75,7 @@ empty() function is used to check if the stack container is empty or not. If the ``` `OUTPUT : FILLED` -In this code we gave a input in MyStack, so the empty() function returned 1. As a result we got "FILLED" as an output. +In this code we gave an input in MyStack, so the empty() function returned 1. As a result we got "FILLED" as an output. Let's see another code: ```cpp @@ -82,10 +83,10 @@ Let's see another code: MyStack.push(5); MyStack.push(15); MyStack.push(63); - //the stack is {5, 15, 63} now - while(MyStack.empty() != 1){ //the loop runs untill empty() function returns 1 - cout << MyStack.top() <<" "; //prints the last element of the stack - MyStack.pop(); //decreasing the size of the stack by removing the last element + //the stack is {5, 15, 63} now + while(MyStack.empty() != 1){ //the loop runs untill empty() function returns 1 + cout << MyStack.top() <<" "; //prints the last element of the stack + MyStack.pop(); //decreasing the size of the stack by removing the last element } ``` `OUTPUT : 5 15 63` @@ -110,6 +111,6 @@ So these were the basic concepts of stack. We must need to solve stack related p - [*UVA 514*](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455) - [*UVA 1062*](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3503) -- [*Codeforces 5C*](https://codeforces.com/contest/5/status) +- [*Codeforces 5C*](https://codeforces.com/contest/5/problem/C) Happy coding<3 From 53d49392f1c18eb5505206cb6ee727f1bb382d94 Mon Sep 17 00:00:00 2001 From: Samiul Islam Mugdha <67468157+mugdha-samiul@users.noreply.github.com> Date: Mon, 29 Jun 2020 06:32:55 +0600 Subject: [PATCH 24/97] Update stack.md --- STL/stack.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/STL/stack.md b/STL/stack.md index 31f73d6..f9afb64 100644 --- a/STL/stack.md +++ b/STL/stack.md @@ -25,7 +25,8 @@ There are mainly four functions which are hugely used in stack. They are: - pop() - top() - empty() -##### push(): + +#### push(): push() function is used to input a data in the stack. This function always adds a new data on the top of the container and increases the size of the stack by one. Example: ```cpp stack < int > MyStack; @@ -35,7 +36,7 @@ push() function is used to input a data in the stack. This function always adds ``` In this code, we have inputed some data in "MyStack". After pushing some datas the stack will be {5, 15, 63}. As '63' is the last data we have inserted, so it is on the top of the stack. -##### pop(): +#### pop(): pop() is the opposite of the push() function. It removes the last data inserted in the stack. It decreases the size of the stack by one.The items are popped in the reversed order in which they are pushed. ```cpp stack < int > MyStack; @@ -74,7 +75,7 @@ empty() function is used to check if the stack container is empty or not. If the ``` `OUTPUT : FILLED` -In this code we gave a input in MyStack, so the empty() function returned 1. As a result we got "FILLED" as an output. +In this code we gave an input in MyStack, so the empty() function returned 1. As a result we got "FILLED" as an output. Let's see another code: ```cpp @@ -82,10 +83,10 @@ Let's see another code: MyStack.push(5); MyStack.push(15); MyStack.push(63); - //the stack is {5, 15, 63} now - while(MyStack.empty() != 1){ //the loop runs untill empty() function returns 1 - cout << MyStack.top() <<" "; //prints the last element of the stack - MyStack.pop(); //decreasing the size of the stack by removing the last element + //the stack is {5, 15, 63} now + while(MyStack.empty() != 1){ //the loop runs untill empty() function returns 1 + cout << MyStack.top() <<" "; //prints the last element of the stack + MyStack.pop(); //decreasing the size of the stack by removing the last element } ``` `OUTPUT : 5 15 63` @@ -110,6 +111,6 @@ So these were the basic concepts of stack. We must need to solve stack related p - [*UVA 514*](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455) - [*UVA 1062*](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3503) -- [*Codeforces 5C*](https://codeforces.com/contest/5/status) +- [*Codeforces 5C*](https://codeforces.com/contest/5/problem/C) Happy coding<3 From acc6878e547b7ae12efa2fb06783f0e55b617135 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 12:03:21 +0600 Subject: [PATCH 25/97] Update PriorityQueue.md --- STL/PriorityQueue.md | 51 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 6b80e30..63d36e2 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -57,14 +57,14 @@ You can do the following operation in a priority queue : *** ## Printing a Priority Queue using a custom function : There are no library function to print a queue without popping. So, we will write a custom function of oue own to print a priority queue. We will send a copy of our priority queue in order to print it. We can pop that copy because it will not change the main Priority_queue which is inside the main function. So, Lets see the code : - ```cpp - #include - #include - - using namespace std; - - void print_priority_queue(priority_queue < int > temp) - { + ```cpp + #include + #include + + using namespace std; + + void print_priority_queue(priority_queue < int > temp) + { cout << "Elements of the priority_queue are :: \n" ; while ( !temp.empty() ) { @@ -72,24 +72,24 @@ There are no library function to print a queue without popping. So, we will wri temp.pop(); } cout << '\n'; - } + } - int main(){ + int main(){ priority_queue < int > MyPQ; - MyPQ.push(12); + MyPQ.push(12); MyPQ.push(18); - MyPQ.push(8); - // After Pushing the queue will be 18, 12, 8 - print_priority_queue(MyPQ); - // printing the priority queue using custom function - // Here we are sending a copy of the queue - MyPQ.pop(); // popping 18 - MyPQ.pop(); // popping 12 - MyPQ.pop(); // popping 8 - // Now the queue will be empty - return 0; - } - ``` + MyPQ.push(8); + // After Pushing the queue will be 18, 12, 8 + print_priority_queue(MyPQ); + // printing the priority queue using custom function + // Here we are sending a copy of the queue + MyPQ.pop(); // popping 18 + MyPQ.pop(); // popping 12 + MyPQ.pop(); // popping 8 + // Now the queue will be empty + return 0; + } + ``` ### OUTPUT: ``` Elements of the priority_queue are :: @@ -98,12 +98,13 @@ There are no library function to print a queue without popping. So, we will wri ## Set the Minimum Number as priority ( Minimum Number Heap ) Sometimes your priority can be to pop the smallest number first. Which means if you want to keep your data in ascending order you need to declare the priority queue using the following code : + ```cpp #include #include - + using namespace std; - + // Function to print minimum heap priority queue void print_spcl_priority_queue(priority_queue < int , vector < int > , greater < int > > temp) { From 416108e2a7c0c1bacdf8b8b8b7dff13d6b06ff68 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 12:11:39 +0600 Subject: [PATCH 26/97] Fixed md codes Fixed md codes to show in the site correctly --- STL/PriorityQueue.md | 1 + 1 file changed, 1 insertion(+) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 63d36e2..a22d877 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -24,6 +24,7 @@ You can do the following operation in a priority queue : - pop - top *** +## Implementation of the operations + _**PUSH :**_ This operation takes an input of certain data type and pushes it into your priority queue or heap. It works same as pushing something in a heap. It take logN time to do this operation. Suppose you want to push 12, 18, 8 into your priority queue. You can do that using the following code: ```cpp priority_queue < int > MyPQ; From c82805b471d274f045882dcd17ae7538be341d34 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 12:13:40 +0600 Subject: [PATCH 27/97] Removed hr Removed hr because it failed to render in the site --- STL/PriorityQueue.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index a22d877..6f9740e 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -23,7 +23,7 @@ You can do the following operation in a priority queue : - push - pop - top -*** + ## Implementation of the operations + _**PUSH :**_ This operation takes an input of certain data type and pushes it into your priority queue or heap. It works same as pushing something in a heap. It take logN time to do this operation. Suppose you want to push 12, 18, 8 into your priority queue. You can do that using the following code: ```cpp From 684b3fa740d12cafd920d122bfb40c5e775df0b7 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 12:19:30 +0600 Subject: [PATCH 28/97] fixed render in site there was a unwanted *** after top --- STL/PriorityQueue.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/STL/PriorityQueue.md b/STL/PriorityQueue.md index 6f9740e..3275987 100644 --- a/STL/PriorityQueue.md +++ b/STL/PriorityQueue.md @@ -20,9 +20,11 @@ This creates a priority queue nammed MyPriorityQueue of a cerrain data type. ## Operations You can do the following operation in a priority queue : -- push -- pop -- top ++ push ++ pop ++ top
+ +Now lets see the implementation ## Implementation of the operations + _**PUSH :**_ This operation takes an input of certain data type and pushes it into your priority queue or heap. It works same as pushing something in a heap. It take logN time to do this operation. Suppose you want to push 12, 18, 8 into your priority queue. You can do that using the following code: From 0059d9551753f1cff1740443468b099d59dcd427 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Mon, 29 Jun 2020 14:12:24 +0600 Subject: [PATCH 29/97] Update vector.md --- STL/vector.md | 266 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 245 insertions(+), 21 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index bd40048..0e3a702 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -1,45 +1,269 @@ # Vector + ## Introduction + Vector is one of the most powerful Data Type in ***C++ programming language*** and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s ***Array***. However, in C++, we call the vector ***“A Container”***. A container can contain everything such as *`integers, doubles, characters, bools, strings, structures`* and a vector can contain *`some vectors`* too. But the most important feature of vector is –***“Its memory is allocated dynamically.”*** We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way. +

+ ## Header File + We have to include an extra header file to use this data type. That is-
`#include `
Under this header file, we can access all the library functions that can be used for vector data type.

+ ## Declaration and initialization of a vector -We can declare a vector in several ways. Let us see them one by one and understand their differences gradually. - - `vector < Datatype > MyVector;`
-Using this format, we can declare a vector. Now we can see that the size of the vector is not mentioned here. That means **“we have not allocated any memory for this vector.”** In this case if we want to add any value (of the datatype mentioned inside), we have to use a library function `push_back()`. For example, if we want to store 55, 72, 89 and 100 into an integer vector, we have to perform these: + +We can declare a vector in several ways. Let us see some of them at a glance and then move forward. + +- Type-01 - ***Creating an empty Vector*** + +```c++ + vector < int > v; +``` + +- Type-02 - ***Specifying size and initializing all values*** + - there are two criterias + - 1 + ```cpp + int n = 7; + vector < int > v(n); + + // Print the vector just to be made sure + cout << "v = "; + for(int x : v) { + cout << x << " "; + } + cout << endl; + ``` + + - This sets all elements to 0 automatically. + + - 2 + + ```cpp + int n = 7; + vector < int > v(n, 10); //The 7 elements of the vector is now 10 + + // Print the vector just to be made sure + cout << "v = "; + for(int x : v) + cout << x << " "; + cout << endl; + ``` + +- Type-03 - **Initializing like arrays** + - We can do it in 2 different styles + - Style-A + + ```cpp + vector < char > v {'d', 'e', 'f', 'i', 'n', 'e', 'c', 'o', 'd', 'e', 'r'}; + + // Print the vector just to be made sure + cout << "v = "; + for(char x : v) { + cout << x; + } + cout << endl; + ``` + + - Style-B - We can use an equal sign too + + ```cpp + vector < double > v = {'d', 'e', 'f', 'i', 'n', 'e', 'c', 'o', 'd', 'e', 'r'}; + + // Print the vector just to be made sure + cout << "v = "; + for(char x : v) + cout << x; + cout << endl; + ``` + +- Type-04 - **Initializing from an array** + ```cpp + int arr[] = {10, 20, 30}; + int n = sizeof(arr) / sizeof(arr[0]); + vector < int > v(arr, arr + n); + + // Print the vector just to be made sure + cout << "v = "; + for(int x : v) + cout << x << " "; + cout << endl; + ``` + +- Type-05 - ***Initializing from another vector*** + ```cpp + vector < int > motherVector {8, 4, 2, 0, 1}; + vector < int > v(motherVector.begin(), motherVector.end()); + + // Print the vector just to be made sure + cout << "v = "; + for(int x : v) + cout << x << " "; + cout << endl; + ``` + +### What operations can we make with an empty vector? + +- We can push elements into it using `push_back()` function. The syntax is like `VariableName.push_back(value)`. We can push as much elements as we need (but within the range). + +```cpp + vector < char > v, v.push_back('d'), v.push_back('e'), v.push_back('f'); + v.push_back('i'), v.push_back('n'), v.push_back('e'); + //And so on... + + // Print the vector just to be made sure + cout << "v = "; + for(char x : v) { + cout << x; + } + cout << endl; +``` + +- The most important thing, when we declare an empty vector, is to have in mind that after declaration, the size of the vector is 0. Then when we push elements one by one, the size of the vector gradually increases. For example, in the previous code - **`"after initialization, the size was 0. Then we pushed 6 elements into the vector. Now The size of the vector is 6."`** + +- We can check the size of the vector at any time using `size()` function. The syntax is `VariableName.size()`. Compile and run the following code to make it clear. + +```cpp + vector < double > v; + cout << "After initialization, size = " << v.size() << endl; + v.push_back(10.5); + cout << "After adding first element, size = " << v.size() << endl; + v.push_back(13.684); + cout << "After adding one more element, size = " << v.size() << endl; + v.push_back(18.12345); + cout << "After adding one more element, size = " << v.size() << endl; +``` + + Output: + After initialization, size = 0 + After adding first element, size = 1 + After adding one more element, size = 2 + After adding one more element, size = 3 + +- We can also assign values to the vector after declaration as shown below. + ```cpp vector < int > v; - v.push_back(55); - v.push_back(72); - v.push_back(89); - v.push_back(100); + v = {10, 20, 30, 40}; ``` -Alternatively, we can write them also in a single line using commas, like this- + +### Which things should we be aware of after declaring an emtpy vector? + +- We cannot add elements without using `push_back()` function. +- We cannot just make the following operations: + ```cpp vector < int > v; - v.push_back(55), v.push_back(72), v.push_back(89), v.push_back(100); + v[0] = 10; + v[1] = 20; + v[2] = 30; ``` -The size of vector `v` will be as much element we insert into the vector using `push_back()` function. - - `vector < Datatype > MyVector(n);` where n = size of the vector.
-Thus we can initially allocate the memory needed for our vector like this. This method will give us two extra benefit: - - This will set the values of all index to 0 automatically. - - We can now access any index form 0 to size-1 inclusively by
`“MyVector[i]”`; where i = index.
-But still we can change the size of the vector at any time. Besides, we can also store more than n elements into the vector (if needed). Using `push_back()`, we can add elements from index n to maximum size. -Suppose, we are declaring a vector of length 3 and store 3 values into the vector. we have to do it like- +or, + ```cpp - vector < int > v(3); - v[0] = 29, v[1] = 37, v[2] = 23; + vector < int > v; + for(int i = 0; i < 10; i++) { + cin >> v[i]; + } +``` + +- We will get a **runtime error** as we are accessing a memory you do not have. The question is why? The answer is quite simple - **"An empty vector does not provide any memory for its elements. When we use `push_back()` function to add new elements, it provides memory for the elements."** But if we want to add some memories to our vector we have to use `resize()` function. The syntax is `VariableName.resize(NewSize)`. **Then we can access indexs form 0 to NewSize-1.** +- So we have to use `v.Push_back(10), v.push_back(20), v.push_back(30)` in the first one. We can handle the second one like this: + +```cpp + vector < int > v; + for(int i = 0; i < 10; i++) { + int x; + cin >> x; + v.push_back(x); + } ``` -Now if we want to add more elements, we can do it by simply using `push_back()`.
+- Or we can use resize the vector and access indexs from 0 to NewSize-1. + ```cpp - v.push_back(18), v.push_back(97); + vector < int > v; + v.resize(10); + for(int i = 0; i < 10; i++) { + cin >> v[i]; + } ``` -**Now the size of the vector is increased from three to five**. + +- Suppose, we have declared an empty vector and then pushed 5 elements into it. After those pushing, we can change any of those value using `VariableName[index] = value`. See the following code: + +```cpp + vector < int > v; + v.push_back(10), v.push_back(11), v.push_back(12), v.push_back(13), v.push_back(14); + v[0] = 100; + v[1] = 200; + v[2] = 300, v[3] = 400, v[4] = 500; +``` + +- So, the lesson is **"We cannot access an index that has no allocation in the memory."** + +*** + +### What happens when we use `resize()` function? + +- Suppose we have a vector of size 5. Now we use `resize()` function to change the size of the vector to 10. After this operation, the first elements of our vector remains the same as it was before. Besides, we get (10 - 5 =) 5 extra indexes to access. Initially these new 5 indexes will contain 0 for an integer vector! Run the code: + +```cpp + vector < int > v{1, 2, 3, 4, 5}; + v.resize(10); + for(int x : v){ + cout << x << " "; + } + cout << endl; +``` + + Output: + 1 2 3 4 5 0 0 0 0 0 + +- The big question in our mind is **What happens after resize, if the data type is not integer!** The answer is **If the datatype is `int`, `double`, `long int` or number related anything, the value is automatically set to 0. And if the data type is `char` or `string` related something, each index will contain a `NULL` character!** + +- Now suppose, we have a vector of length 7. We resize if to length 5. Then these 5 elements remains the same. Just those last elements gets deleted. + +```cpp + vector < int > v{1, 2, 3, 4, 5, 6, 7}; + v.resize(5); + for(int x : v){ + cout << x << " "; + } + cout << endl; +``` + Output: + 1 2 3 4 5 + +### What if we want all of the elements of our vector become 0 (for number types) or NULL (for character types) after resize? + +- Use `clear()` function before resize. The syntax is `VariableName.clear()`. + +```cpp + vector < int > v{1, 2, 3, 4, 5, 6, 7}; + v.clear(); + v.resize(10); + for(int x : v){ + cout << x << " "; + } + cout << endl; +``` + + Output: + 0 0 0 0 0 0 0 0 0 0 + +- **`clear()` function deletes all the elements of the vector and the vector becomes empty.** + + + + + + + + + From ed87d09f8d70fd2e8f5343891ae2fb68a8ae68fb Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Mon, 29 Jun 2020 14:33:42 +0600 Subject: [PATCH 30/97] Update vector.md --- STL/vector.md | 204 ++++++++++++++++++++++++-------------------------- 1 file changed, 98 insertions(+), 106 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 0e3a702..4d89a57 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -21,9 +21,9 @@ We can declare a vector in several ways. Let us see some of them at a glance and - Type-01 - ***Creating an empty Vector*** -```c++ + ```c++ vector < int > v; -``` + ``` - Type-02 - ***Specifying size and initializing all values*** - there are two criterias @@ -111,99 +111,99 @@ We can declare a vector in several ways. Let us see some of them at a glance and - We can push elements into it using `push_back()` function. The syntax is like `VariableName.push_back(value)`. We can push as much elements as we need (but within the range). -```cpp - vector < char > v, v.push_back('d'), v.push_back('e'), v.push_back('f'); - v.push_back('i'), v.push_back('n'), v.push_back('e'); - //And so on... + ```cpp + vector < char > v, v.push_back('d'), v.push_back('e'), v.push_back('f'); + v.push_back('i'), v.push_back('n'), v.push_back('e'); + //And so on... - // Print the vector just to be made sure - cout << "v = "; - for(char x : v) { - cout << x; - } - cout << endl; -``` + // Print the vector just to be made sure + cout << "v = "; + for(char x : v) { + cout << x; + } + cout << endl; + ``` - The most important thing, when we declare an empty vector, is to have in mind that after declaration, the size of the vector is 0. Then when we push elements one by one, the size of the vector gradually increases. For example, in the previous code - **`"after initialization, the size was 0. Then we pushed 6 elements into the vector. Now The size of the vector is 6."`** - We can check the size of the vector at any time using `size()` function. The syntax is `VariableName.size()`. Compile and run the following code to make it clear. -```cpp - vector < double > v; - cout << "After initialization, size = " << v.size() << endl; - v.push_back(10.5); - cout << "After adding first element, size = " << v.size() << endl; - v.push_back(13.684); - cout << "After adding one more element, size = " << v.size() << endl; - v.push_back(18.12345); - cout << "After adding one more element, size = " << v.size() << endl; -``` - - Output: - After initialization, size = 0 - After adding first element, size = 1 - After adding one more element, size = 2 - After adding one more element, size = 3 + ```cpp + vector < double > v; + cout << "After initialization, size = " << v.size() << endl; + v.push_back(10.5); + cout << "After adding first element, size = " << v.size() << endl; + v.push_back(13.684); + cout << "After adding one more element, size = " << v.size() << endl; + v.push_back(18.12345); + cout << "After adding one more element, size = " << v.size() << endl; + ``` + + Output: + After initialization, size = 0 + After adding first element, size = 1 + After adding one more element, size = 2 + After adding one more element, size = 3 - We can also assign values to the vector after declaration as shown below. -```cpp - vector < int > v; - v = {10, 20, 30, 40}; -``` + ```cpp + vector < int > v; + v = {10, 20, 30, 40}; + ``` ### Which things should we be aware of after declaring an emtpy vector? - We cannot add elements without using `push_back()` function. - We cannot just make the following operations: -```cpp - vector < int > v; - v[0] = 10; - v[1] = 20; - v[2] = 30; -``` + ```cpp + vector < int > v; + v[0] = 10; + v[1] = 20; + v[2] = 30; + ``` or, -```cpp - vector < int > v; - for(int i = 0; i < 10; i++) { - cin >> v[i]; - } -``` + ```cpp + vector < int > v; + for(int i = 0; i < 10; i++) { + cin >> v[i]; + } + ``` - We will get a **runtime error** as we are accessing a memory you do not have. The question is why? The answer is quite simple - **"An empty vector does not provide any memory for its elements. When we use `push_back()` function to add new elements, it provides memory for the elements."** But if we want to add some memories to our vector we have to use `resize()` function. The syntax is `VariableName.resize(NewSize)`. **Then we can access indexs form 0 to NewSize-1.** - So we have to use `v.Push_back(10), v.push_back(20), v.push_back(30)` in the first one. We can handle the second one like this: -```cpp - vector < int > v; - for(int i = 0; i < 10; i++) { - int x; - cin >> x; - v.push_back(x); - } -``` + ```cpp + vector < int > v; + for(int i = 0; i < 10; i++) { + int x; + cin >> x; + v.push_back(x); + } + ``` - Or we can use resize the vector and access indexs from 0 to NewSize-1. -```cpp - vector < int > v; - v.resize(10); - for(int i = 0; i < 10; i++) { - cin >> v[i]; - } -``` + ```cpp + vector < int > v; + v.resize(10); + for(int i = 0; i < 10; i++) { + cin >> v[i]; + } + ``` - Suppose, we have declared an empty vector and then pushed 5 elements into it. After those pushing, we can change any of those value using `VariableName[index] = value`. See the following code: -```cpp - vector < int > v; - v.push_back(10), v.push_back(11), v.push_back(12), v.push_back(13), v.push_back(14); - v[0] = 100; - v[1] = 200; - v[2] = 300, v[3] = 400, v[4] = 500; -``` + ```cpp + vector < int > v; + v.push_back(10), v.push_back(11), v.push_back(12), v.push_back(13), v.push_back(14); + v[0] = 100; + v[1] = 200; + v[2] = 300, v[3] = 400, v[4] = 500; + ``` - So, the lesson is **"We cannot access an index that has no allocation in the memory."** @@ -213,57 +213,49 @@ or, - Suppose we have a vector of size 5. Now we use `resize()` function to change the size of the vector to 10. After this operation, the first elements of our vector remains the same as it was before. Besides, we get (10 - 5 =) 5 extra indexes to access. Initially these new 5 indexes will contain 0 for an integer vector! Run the code: -```cpp - vector < int > v{1, 2, 3, 4, 5}; - v.resize(10); - for(int x : v){ - cout << x << " "; - } - cout << endl; -``` + ```cpp + vector < int > v{1, 2, 3, 4, 5}; + v.resize(10); + for(int x : v){ + cout << x << " "; + } + cout << endl; + ``` - Output: - 1 2 3 4 5 0 0 0 0 0 + Output: + 1 2 3 4 5 0 0 0 0 0 - The big question in our mind is **What happens after resize, if the data type is not integer!** The answer is **If the datatype is `int`, `double`, `long int` or number related anything, the value is automatically set to 0. And if the data type is `char` or `string` related something, each index will contain a `NULL` character!** - Now suppose, we have a vector of length 7. We resize if to length 5. Then these 5 elements remains the same. Just those last elements gets deleted. -```cpp - vector < int > v{1, 2, 3, 4, 5, 6, 7}; - v.resize(5); - for(int x : v){ - cout << x << " "; - } - cout << endl; -``` - Output: - 1 2 3 4 5 + ```cpp + vector < int > v{1, 2, 3, 4, 5, 6, 7}; + v.resize(5); + for(int x : v){ + cout << x << " "; + } + cout << endl; + ``` + Output: + 1 2 3 4 5 ### What if we want all of the elements of our vector become 0 (for number types) or NULL (for character types) after resize? - Use `clear()` function before resize. The syntax is `VariableName.clear()`. -```cpp - vector < int > v{1, 2, 3, 4, 5, 6, 7}; - v.clear(); - v.resize(10); - for(int x : v){ - cout << x << " "; - } - cout << endl; -``` + ```cpp + vector < int > v{1, 2, 3, 4, 5, 6, 7}; + v.clear(); + v.resize(10); + for(int x : v){ + cout << x << " "; + } + cout << endl; + ``` - Output: - 0 0 0 0 0 0 0 0 0 0 + Output: + 0 0 0 0 0 0 0 0 0 0 - **`clear()` function deletes all the elements of the vector and the vector becomes empty.** - - - - - - - - From 03fd8f17e1b08a38e2ee780891125b9f00ea3afd Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Mon, 29 Jun 2020 14:37:17 +0600 Subject: [PATCH 31/97] Update vector.md --- STL/vector.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index 4d89a57..50560c0 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -107,6 +107,8 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << endl; ``` +*** + ### What operations can we make with an empty vector? - We can push elements into it using `push_back()` function. The syntax is like `VariableName.push_back(value)`. We can push as much elements as we need (but within the range). @@ -152,6 +154,8 @@ We can declare a vector in several ways. Let us see some of them at a glance and v = {10, 20, 30, 40}; ``` +*** + ### Which things should we be aware of after declaring an emtpy vector? - We cannot add elements without using `push_back()` function. @@ -164,7 +168,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and v[2] = 30; ``` -or, + or, ```cpp vector < int > v; @@ -240,6 +244,8 @@ or, Output: 1 2 3 4 5 +*** + ### What if we want all of the elements of our vector become 0 (for number types) or NULL (for character types) after resize? - Use `clear()` function before resize. The syntax is `VariableName.clear()`. @@ -259,3 +265,4 @@ or, - **`clear()` function deletes all the elements of the vector and the vector becomes empty.** +*** From b184708cfe371e305f61f4828279ffc3ede34de1 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Mon, 29 Jun 2020 14:47:28 +0600 Subject: [PATCH 32/97] Update vector.md --- STL/vector.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 50560c0..263ccc4 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -55,7 +55,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << endl; ``` -- Type-03 - **Initializing like arrays** +- Type-03 - ***Initializing like arrays*** - We can do it in 2 different styles - Style-A @@ -82,7 +82,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << endl; ``` -- Type-04 - **Initializing from an array** +- Type-04 - ***Initializing from an array*** ```cpp int arr[] = {10, 20, 30}; int n = sizeof(arr) / sizeof(arr[0]); @@ -177,7 +177,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and } ``` -- We will get a **runtime error** as we are accessing a memory you do not have. The question is why? The answer is quite simple - **"An empty vector does not provide any memory for its elements. When we use `push_back()` function to add new elements, it provides memory for the elements."** But if we want to add some memories to our vector we have to use `resize()` function. The syntax is `VariableName.resize(NewSize)`. **Then we can access indexs form 0 to NewSize-1.** +- We will get a **runtime error** as we are accessing a memory we do not have. The question is why? The answer is quite simple - **"An empty vector does not provide any memory for its elements. When we use `push_back()` function to add new elements, it provides memory for the elements."** But if we want to add some memories to our vector we have to use `resize()` function. The syntax is `VariableName.resize(NewSize)`. **Then we can access indexs form 0 to NewSize-1.** - So we have to use `v.Push_back(10), v.push_back(20), v.push_back(30)` in the first one. We can handle the second one like this: ```cpp From d4784f389ec42cd9a2565ea1a0137d5fa76cfe66 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Mon, 29 Jun 2020 15:03:00 +0600 Subject: [PATCH 33/97] Update vector.md --- STL/vector.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 263ccc4..52ae401 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -5,15 +5,22 @@ Vector is one of the most powerful Data Type in ***C++ programming language*** and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s ***Array***. However, in C++, we call the vector ***“A Container”***. A container can contain everything such as *`integers, doubles, characters, bools, strings, structures`* and a vector can contain *`some vectors`* too. But the most important feature of vector is –***“Its memory is allocated dynamically.”*** We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way.
-
+ +*** + +### Why should we learn vector? +- We can change the size of a vector at any time +- We can delete any element at any time +- We can insert new elements at any place +- We can set all the value of all elements to an specific value without using loops +- We can copy one vector to another without using loops +- And so many things we can perform using vector which is not possible using traditional array + +*** ## Header File -We have to include an extra header file to use this data type. That is-
-`#include `
-Under this header file, we can access all the library functions that can be used for vector data type. -
-
+We have to include an extra header file to use this data type. That is- `#include `. Under this header file, we can access all the library functions that can be used for vector data type. ## Declaration and initialization of a vector From f6000c3d50e40b68cea4897db0161c06f14d992e Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Mon, 29 Jun 2020 15:05:27 +0600 Subject: [PATCH 34/97] Update vector.md --- STL/vector.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 52ae401..fb44722 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -4,8 +4,6 @@ Vector is one of the most powerful Data Type in ***C++ programming language*** and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s ***Array***. However, in C++, we call the vector ***“A Container”***. A container can contain everything such as *`integers, doubles, characters, bools, strings, structures`* and a vector can contain *`some vectors`* too. But the most important feature of vector is –***“Its memory is allocated dynamically.”*** We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way. -
- *** ### Why should we learn vector? From 8e1ce0d49d4c5e6a1769ee9c6c1b3a6955acc73c Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Mon, 29 Jun 2020 22:01:33 +0600 Subject: [PATCH 35/97] update set - added title - added declaration code - added basic operation - added isertion --- STL/set.md | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index 14780cc..cb5b17c 100644 --- a/STL/set.md +++ b/STL/set.md @@ -1,4 +1,4 @@ -# Set +# Set in C++ ## Introduction Set is a container which has very special for its functionality. This have some variants which are also discussed here later on. The main features of default set is : @@ -14,4 +14,47 @@ First of all you need to add the header file of set to use any functionality of After adding the header you can declare your set using this code: ```cpp set < data_type > MySet; +// For example you may declare a set of integers nemmed MyIntSet using : +set < int > MyIntSet; ``` +In the place of data_type you may use any data type like integer, double, charecter, string, pair etc. + +## Basic operations +The often used operations in set are : +- Insert +- Iterator +- Printing a set +- Erase +- Size +- Find and Count +- Swap +- Upper and Lower Bound + +## Implementation of the basic operations in set +1. **INSERT :** Insertion is a very basic operation of set. Using this operation you can insert a new element into your set. But remember insersion will add a new element if that element was absent there before because set do not allows same element's occurance twice. So lets see the implementation : + ```cpp + #include + #include + + using namespace std; + + int main(){ + + set < int > MySet; // Declaring MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(45); + // 45 is already in the set so nothing happens + + return 0; + } + ``` +2. **ITERATOR :** +3. **PRINTING A SET :** +4. **ERASE :** +5. **SIZE :** +6. **FIND AND COUNT :** +7. **SWAP :** +8. **UPPER AND LOWER :** From 6f7e1603f9571c843dca9e0c631325b1172435aa Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:11:45 +0600 Subject: [PATCH 36/97] Update vector.md --- STL/vector.md | 1 + 1 file changed, 1 insertion(+) diff --git a/STL/vector.md b/STL/vector.md index fb44722..245600b 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -37,6 +37,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and int n = 7; vector < int > v(n); + // This sets all elements to 0 automatically // Print the vector just to be made sure cout << "v = "; for(int x : v) { From 4b4e4d122c161f3a8e898c8c56684d75d7ceee2d Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:16:44 +0600 Subject: [PATCH 37/97] Update vector.md --- STL/vector.md | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 245600b..b264b14 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -45,6 +45,9 @@ We can declare a vector in several ways. Let us see some of them at a glance and } cout << endl; ``` + + Output: + 0 0 0 0 0 0 0 - This sets all elements to 0 automatically. @@ -60,7 +63,11 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << x << " "; cout << endl; ``` - + + Output: + 10 10 10 10 10 10 10 + + - Type-03 - ***Initializing like arrays*** - We can do it in 2 different styles - Style-A @@ -76,6 +83,10 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << endl; ``` + Output: + definecoder + + - Style-B - We can use an equal sign too ```cpp @@ -87,7 +98,11 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << x; cout << endl; ``` - + + Output: + definecoder + + - Type-04 - ***Initializing from an array*** ```cpp int arr[] = {10, 20, 30}; @@ -100,7 +115,11 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << x << " "; cout << endl; ``` - + + Output: + 10 20 30 + + - Type-05 - ***Initializing from another vector*** ```cpp vector < int > motherVector {8, 4, 2, 0, 1}; @@ -112,6 +131,10 @@ We can declare a vector in several ways. Let us see some of them at a glance and cout << x << " "; cout << endl; ``` + + Output: + 8 4 2 0 1 + *** @@ -131,6 +154,10 @@ We can declare a vector in several ways. Let us see some of them at a glance and } cout << endl; ``` + + Output: + define + - The most important thing, when we declare an empty vector, is to have in mind that after declaration, the size of the vector is 0. Then when we push elements one by one, the size of the vector gradually increases. For example, in the previous code - **`"after initialization, the size was 0. Then we pushed 6 elements into the vector. Now The size of the vector is 6."`** From 44d1a72e5764b06c2cdc63fbc0891316d23ce87c Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:24:27 +0600 Subject: [PATCH 38/97] Update vector.md --- STL/vector.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index b264b14..61d888d 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -143,7 +143,8 @@ We can declare a vector in several ways. Let us see some of them at a glance and - We can push elements into it using `push_back()` function. The syntax is like `VariableName.push_back(value)`. We can push as much elements as we need (but within the range). ```cpp - vector < char > v, v.push_back('d'), v.push_back('e'), v.push_back('f'); + vector < char > v; + v.push_back('d'), v.push_back('e'), v.push_back('f'); v.push_back('i'), v.push_back('n'), v.push_back('e'); //And so on... From 05f7a39c97cd7f6799259afea526a633bd9ea7af Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:33:38 +0600 Subject: [PATCH 39/97] Update vector.md --- STL/vector.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 61d888d..c64251d 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -211,7 +211,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and } ``` -- We will get a **runtime error** as we are accessing a memory we do not have. The question is why? The answer is quite simple - **"An empty vector does not provide any memory for its elements. When we use `push_back()` function to add new elements, it provides memory for the elements."** But if we want to add some memories to our vector we have to use `resize()` function. The syntax is `VariableName.resize(NewSize)`. **Then we can access indexs form 0 to NewSize-1.** +- We will get a **runtime error** as we are accessing a memory that we do not have. The question is why? The answer is quite simple - **"An empty vector does not provide any memory for its elements. When we use `push_back()` function to add new elements, it provides memory for the elements."** But if we want to add memories for some elements to our vector, we have to use `resize()` function. The syntax is `VariableName.resize(NewSize)`. **Then we can access indexs form 0 to NewSize - 1.** - So we have to use `v.Push_back(10), v.push_back(20), v.push_back(30)` in the first one. We can handle the second one like this: ```cpp @@ -223,7 +223,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and } ``` -- Or we can use resize the vector and access indexs from 0 to NewSize-1. +- Or we can use resize the vector and access indexs from 0 to NewSize - 1. ```cpp vector < int > v; @@ -263,9 +263,9 @@ We can declare a vector in several ways. Let us see some of them at a glance and Output: 1 2 3 4 5 0 0 0 0 0 -- The big question in our mind is **What happens after resize, if the data type is not integer!** The answer is **If the datatype is `int`, `double`, `long int` or number related anything, the value is automatically set to 0. And if the data type is `char` or `string` related something, each index will contain a `NULL` character!** +- The big question in our mind is **What happens after resize if the data type is not integer!** The answer is **If the datatype is `int`, `double`, `long int` or number related anything, the value is automatically set to 0. And if the data type is `char` or `string` related something, each index will contain a `NULL` character!** -- Now suppose, we have a vector of length 7. We resize if to length 5. Then these 5 elements remains the same. Just those last elements gets deleted. +- Now suppose, we have a vector of length 7. We resize if to length 5. Then these 5 elements remains the same. Just those last elements get deleted. ```cpp vector < int > v{1, 2, 3, 4, 5, 6, 7}; @@ -282,7 +282,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and ### What if we want all of the elements of our vector become 0 (for number types) or NULL (for character types) after resize? -- Use `clear()` function before resize. The syntax is `VariableName.clear()`. +- Use `clear()` function before resize. The syntax of `clear()` function is `VariableName.clear()`. ```cpp vector < int > v{1, 2, 3, 4, 5, 6, 7}; @@ -297,6 +297,6 @@ We can declare a vector in several ways. Let us see some of them at a glance and Output: 0 0 0 0 0 0 0 0 0 0 -- **`clear()` function deletes all the elements of the vector and the vector becomes empty.** +- **`clear()` function deletes all the elements of the vector and the vector becomes empty. And when we resize an empty vector, all indexes will contain 0 (or NULL) by default.** *** From a278db7ec346d480b542b7938110eb7645796684 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:36:28 +0600 Subject: [PATCH 40/97] Update vector.md --- STL/vector.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index c64251d..1bfe365 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -166,7 +166,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and ```cpp vector < double > v; - cout << "After initialization, size = " << v.size() << endl; + cout << "After declaration, size = " << v.size() << endl; v.push_back(10.5); cout << "After adding first element, size = " << v.size() << endl; v.push_back(13.684); @@ -176,7 +176,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and ``` Output: - After initialization, size = 0 + After declaration, size = 0 After adding first element, size = 1 After adding one more element, size = 2 After adding one more element, size = 3 From dcaf3035c7cebf3117e3665eab7066e0b6b423b9 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:38:49 +0600 Subject: [PATCH 41/97] Update vector.md --- STL/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index 1bfe365..5e63e69 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -2,7 +2,7 @@ ## Introduction -Vector is one of the most powerful Data Type in ***C++ programming language*** and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s ***Array***. However, in C++, we call the vector ***“A Container”***. A container can contain everything such as *`integers, doubles, characters, bools, strings, structures`* and a vector can contain *`some vectors`* too. But the most important feature of vector is –***“Its memory is allocated dynamically.”*** We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way. +Vector is one of the most powerful Data Type in ***C++ programming language*** and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s ***Array***. However, in C++, we call the vector ***“A Container”***. As a container, a vector can contain everything such as *`integers, doubles, characters, bools, strings, structures`* and a vector can contain *`some vectors`* too. But the most important feature of vector is –***“Its memory is allocated dynamically.”*** We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way. *** From 73629996ae222943e7950a45ee037748abb9f88e Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:40:52 +0600 Subject: [PATCH 42/97] Update vector.md --- STL/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index 5e63e69..5741cae 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -2,7 +2,7 @@ ## Introduction -Vector is one of the most powerful Data Type in ***C++ programming language*** and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s ***Array***. However, in C++, we call the vector ***“A Container”***. As a container, a vector can contain everything such as *`integers, doubles, characters, bools, strings, structures`* and a vector can contain *`some vectors`* too. But the most important feature of vector is –***“Its memory is allocated dynamically.”*** We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way. +Vector is one of the most powerful Data Type in ***C++ programming language*** and very much helpful for competitive programmers. The concept of vector is almost same as C programming language’s ***Array***. However, in C++, we call the vector ***“A Container”***. As a container, a vector can contain everything such as *`integers, doubles, characters, bools, strings, structures`* and a vector can contain *`some vectors`* too. But the most important feature of vector is –***“Its memory is allocated dynamically.”*** We can change its length at any time and you can perform many operations that is not possible for C language’s array. We will see and learn all of these things gradually in a proper way. *** From 92efe3a310d022751eb1c8f9ccaa988ceed39628 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 12:45:09 +0600 Subject: [PATCH 43/97] Update vector.md --- STL/vector.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/STL/vector.md b/STL/vector.md index 5741cae..cf0795e 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -300,3 +300,25 @@ We can declare a vector in several ways. Let us see some of them at a glance and - **`clear()` function deletes all the elements of the vector and the vector becomes empty. And when we resize an empty vector, all indexes will contain 0 (or NULL) by default.** *** + +- The last 4 questions explains how we are going to insert new elements to our vector under different circumstances. Now we will see a list of some functions along with a short description. + +*** + + + + + + + + + + + + + + + + + + From 0feb116c6ee6dac28830edc7aed1161bd677245a Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 13:09:54 +0600 Subject: [PATCH 44/97] Update vector.md --- STL/vector.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/STL/vector.md b/STL/vector.md index cf0795e..cd6a45e 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -305,8 +305,30 @@ We can declare a vector in several ways. Let us see some of them at a glance and *** +### Important Iterator Functions +| Function | Work of the function | +|--------------|:------------------------------------------------------------------------------------------------------------------------| +| **begin()** | Returns an iterator pointing to the first vector | +| **end()** | Returns an iterator pointing to the theoretical element that follows the last element in a vector | +| **rbegin()** | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | +| **rend()** | Returns a reverse iterator pointing to the first element in the vector (reverse end). | +*** + +### Important Capacity Functions + +| Function | Work of the function | +|:--------------------|:----------------------------------------------------------------------------------------------------| +| **size()** | Returns the number of elements in the vector | +| **max_size()** | Returns the maximum number of elements that the vetor can hold | +| **capacity()** | Returns the size of the storage space currently allocated to the vector expressed as number | +| **resize(n)** | Resizes the container so that it can contain `'n'` elements | +| **empty()** | Returns whether the container is empty | +| **shrink_to_fit()** | Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity | +| **reserve(n)** | Requests that the vector capacity be at least enough to contain `'n'` elements | + +*** From 551d221f3564f7fb7c38d07189f3aa936bd79c72 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 13:20:14 +0600 Subject: [PATCH 45/97] Update vector.md --- STL/vector.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index cd6a45e..39fa353 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -309,7 +309,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and | Function | Work of the function | |--------------|:------------------------------------------------------------------------------------------------------------------------| -| **begin()** | Returns an iterator pointing to the first vector | +| **begin()** | Returns an iterator pointing to the first element of the vector | | **end()** | Returns an iterator pointing to the theoretical element that follows the last element in a vector | | **rbegin()** | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | | **rend()** | Returns a reverse iterator pointing to the first element in the vector (reverse end). | @@ -330,7 +330,15 @@ We can declare a vector in several ways. Let us see some of them at a glance and *** +### Element Access Functions +| Function | Work of the function | +|:---------------------------|:-------------------------------------------------------------------------------------------------------| +| **reference operator [g]** | Returns a reference to the element at position `'g'` in the vector | +| **at()** | Returns a reference to the element at position `'g'` in the vector | +| **front()** | Returns a reference to the first element in the vector | +| **beck()** | Returns a reference to the last element of the vector | +| **data()** | Returns a direct pointer to the memory array used internally by the vector to store its owned elements | From cf893912d954bdb61dc1fc97aa10681dd6856ad5 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 13:23:18 +0600 Subject: [PATCH 46/97] Update vector.md --- STL/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index 39fa353..ff9b63f 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -301,7 +301,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and *** -- The last 4 questions explains how we are going to insert new elements to our vector under different circumstances. Now we will see a list of some functions along with a short description. +## The last 4 questions explains how we are going to insert new elements to our vector under different circumstances. Now we will see a list of some functions along with a short description. *** From f7c31283255785a2cfa47bfa2bd58b45d425be96 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 13:38:39 +0600 Subject: [PATCH 47/97] Update vector.md --- STL/vector.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index ff9b63f..a2b1de3 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -308,7 +308,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and ### Important Iterator Functions | Function | Work of the function | -|--------------|:------------------------------------------------------------------------------------------------------------------------| +|--------------|-------------------------------------------------------------------------------------------------------------------------| | **begin()** | Returns an iterator pointing to the first element of the vector | | **end()** | Returns an iterator pointing to the theoretical element that follows the last element in a vector | | **rbegin()** | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | @@ -319,7 +319,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and ### Important Capacity Functions | Function | Work of the function | -|:--------------------|:----------------------------------------------------------------------------------------------------| +|---------------------|-----------------------------------------------------------------------------------------------------| | **size()** | Returns the number of elements in the vector | | **max_size()** | Returns the maximum number of elements that the vetor can hold | | **capacity()** | Returns the size of the storage space currently allocated to the vector expressed as number | @@ -333,15 +333,28 @@ We can declare a vector in several ways. Let us see some of them at a glance and ### Element Access Functions | Function | Work of the function | -|:---------------------------|:-------------------------------------------------------------------------------------------------------| +|----------------------------|--------------------------------------------------------------------------------------------------------| | **reference operator [g]** | Returns a reference to the element at position `'g'` in the vector | | **at()** | Returns a reference to the element at position `'g'` in the vector | | **front()** | Returns a reference to the first element in the vector | | **beck()** | Returns a reference to the last element of the vector | | **data()** | Returns a direct pointer to the memory array used internally by the vector to store its owned elements | +*** + +### Modifiers +| Function | Work of the function | +|-----------------|------------------------------------------------------------------------------------------------| +| **assign()** | It assigns new value to the vector elements by replacing old ones | +| **push_back()** | It pushes the elements into the vector form the back | +| **pop_back()** | It removes elements from a vector from the back | +| **insert()** | It inserts new elements before the element at specified position | +| **erase()** | It removes elements from the specified position or specified range | +| **swap()** | It is used to swap the content of one vector with another vector of same type, size may differ | +| **clear()** | It is used to remove all the elements of the vector container | +*** From 97960dfee1ef0d6a5bcee288195cb4500ab3cce6 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 13:45:37 +0600 Subject: [PATCH 48/97] Update string.md --- STL/string.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/string.md b/STL/string.md index 2e2b842..33ab625 100644 --- a/STL/string.md +++ b/STL/string.md @@ -31,7 +31,7 @@ You can also check which string is lexicographically greater using **` strA > st ## String Functions : | Function | Work of the function | -|:-----------------------:|:------------------------------------------------------------------------------:| +|-------------------------|--------------------------------------------------------------------------------| | MyString.size() | Returns The size of the string | | str1.swap(str2) | Swaps the value of str1 and str2 | | MyString.insert(x,s) | Inserts s in the xth index of MyString | From 61cf7ee2c3b6360db9933e098cbadff45b90881b Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 30 Jun 2020 13:52:41 +0600 Subject: [PATCH 49/97] Update string.md --- STL/string.md | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/STL/string.md b/STL/string.md index 33ab625..af599c9 100644 --- a/STL/string.md +++ b/STL/string.md @@ -30,16 +30,19 @@ You can also check which string is lexicographically greater using **` strA > st ## String Functions : -| Function | Work of the function | -|-------------------------|--------------------------------------------------------------------------------| -| MyString.size() | Returns The size of the string | -| str1.swap(str2) | Swaps the value of str1 and str2 | -| MyString.insert(x,s) | Inserts s in the xth index of MyString | -| MySrting.find(s) | returns initial index of first occurrence of the s in MyString else returns -1 | -| MySrting.rfind(s) | returns initial index of last occurrence of the s in MyString else returns -1 | -| MyString.replace(i,j,s) | Replaces j characters from ith index of MyString with new string s | -| stoi(MyString) | Returns the integer converted form of MyString | -| MyString.substr(i,k) | Returns a substing of length k from ith index of MyString | -| MyString.erase(i) | Erases every element from ith index to the end of MyString | -| MyString.clear() | Erases every element in MyStirng | -| MyString.empty() | Returns True if MyString is empty else return false | +| Function | Work of the function | +|-----------------------------|--------------------------------------------------------------------------------| +| **MyString.size()** | Returns The size of the string | +| **str1.swap(str2)** | Swaps the value of str1 and str2 | +| **MyString.insert(x,s)** | Inserts s in the xth index of MyString | +| **MySrting.find(s)** | Returns initial index of first occurrence of the s in MyString else returns -1 | +| **MySrting.rfind(s)** | Returns initial index of last occurrence of the s in MyString else returns -1 | +| **MyString.replace(i,j,s)** | Replaces j characters from ith index of MyString with new string s | +| **stoi(MyString)** | Returns the integer converted form of MyString | +| **MyString.substr(i,k)** | Returns a substing of length k from ith index of MyString | +| **MyString.erase(i)** | Erases every element from ith index to the end of MyString | +| **MyString.clear()** | Erases every element in MyStirng | +| **MyString.empty()** | Returns True if MyString is empty else return false | + +*** + From 69db6dfba71bb107eb6c155d72d66236cf555e61 Mon Sep 17 00:00:00 2001 From: Shawon Majid Date: Tue, 30 Jun 2020 14:52:34 +0600 Subject: [PATCH 50/97] Update pair.md --- STL/pair.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/STL/pair.md b/STL/pair.md index 944d690..c727bb9 100644 --- a/STL/pair.md +++ b/STL/pair.md @@ -85,4 +85,7 @@ int main() Now that you can make pairs so start solving some problems using pairs! +**problem links:** +1. [geeksforgeeks](https://practice.geeksforgeeks.org/problems/c-stl-set-2-pair/1) + From f66e3c0b00fa0a692a561f7d3742b8ee2619ea71 Mon Sep 17 00:00:00 2001 From: Shawon Majid Date: Tue, 30 Jun 2020 15:03:38 +0600 Subject: [PATCH 51/97] Update pair.md --- STL/pair.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/STL/pair.md b/STL/pair.md index c727bb9..08ed637 100644 --- a/STL/pair.md +++ b/STL/pair.md @@ -83,8 +83,15 @@ int main() ``` **We could also sort the vectors of pairs in the increasing order of id or or name by using customized sort functions, which will be shown in the sorting part of STL** -Now that you can make pairs so start solving some problems using pairs! +## Pair functions: +| Function | work of function | +|-----|-----| +|**make_pair(value1, value2)**| Assigns value in pair| +|**pair1.swap(pair2)** | This function swaps the contents of one pair object with the contents of another pair object. The pairs must be of same type.| + +Now that you can make pairs so start solving some problems using pairs! +
**problem links:** 1. [geeksforgeeks](https://practice.geeksforgeeks.org/problems/c-stl-set-2-pair/1) From 3a3396e88163001bde7167c7b3d85f3c472cbd98 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Tue, 30 Jun 2020 17:13:14 +0600 Subject: [PATCH 52/97] Updated set.md - Added printing set - added printing set by an iterator - Edited insert --- STL/set.md | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/STL/set.md b/STL/set.md index cb5b17c..77dd6c9 100644 --- a/STL/set.md +++ b/STL/set.md @@ -47,12 +47,53 @@ The often used operations in set are : MySet.insert(11); // Inserting 11 MySet.insert(45); // 45 is already in the set so nothing happens - + // So, Now the set should be : 11 , 23 , 45 + return 0; } ``` + Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user. 2. **ITERATOR :** -3. **PRINTING A SET :** +3. **PRINTING A SET :** Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: + - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. + ```cpp + #include + #include + + using namespace std; + + int main(){ + + set < int > MySet; // Declaring MySet + set < int >::iterator it; + // Declaring an iterator to point to the elements of MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 23 , 45 + + cout << "The Elements of the set are : " << endl; + + for( it=MySet.begin() ; it != MySet.end() ; it++ ) + { + // Dereferencing the pointer( Iterator ) to see the value + cout << *it << " "; + } + + cout << endl ; + + return 0; + } + ``` + **OUTPUT :** + ``` + The Elements of the set are : + 11 23 45 + ``` + - _**Special Format of C++11**_ : 4. **ERASE :** 5. **SIZE :** 6. **FIND AND COUNT :** From fb5a41376d2cf3ef6da71ec9168f9e267fe6861d Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Tue, 30 Jun 2020 18:14:38 +0600 Subject: [PATCH 53/97] updated set - added C++11 special printing of set --- STL/set.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index 77dd6c9..0ba8ce1 100644 --- a/STL/set.md +++ b/STL/set.md @@ -93,7 +93,44 @@ The often used operations in set are : The Elements of the set are : 11 23 45 ``` - - _**Special Format of C++11**_ : + So, You can print or access the elemets of a set using iterator. + - _**Special Format of C++11**_ : This mathod is very easy to use and I prefer to use this type. Because, almost all the datatypes or STL containers support this type of iteration or traversal through the set or any container. Lets see the code to see the implementation : + ```cpp + #include + #include + + using namespace std; + + int main(){ + + set < int > MySet; // Declaring MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 23 , 45 + + cout << "The Elements of the set are : " << endl; + + for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one + { + // Here an_element is a member of MySet + cout << an_element << " "; + } + + cout << endl ; + + return 0; + } + ``` + **OUTPUT :** + ``` + The Elements of the set are : + 11 23 45 + ``` + I prefer to use this format because it is simple and easy to use. **BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP** . 4. **ERASE :** 5. **SIZE :** 6. **FIND AND COUNT :** From 243b1bce73709f7b583da58ae7c7d512ac8c09ca Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Tue, 30 Jun 2020 18:58:18 +0600 Subject: [PATCH 54/97] update set.md - added erase - added erase by const value --- STL/set.md | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/STL/set.md b/STL/set.md index 0ba8ce1..4b9d150 100644 --- a/STL/set.md +++ b/STL/set.md @@ -131,8 +131,45 @@ The often used operations in set are : 11 23 45 ``` I prefer to use this format because it is simple and easy to use. **BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP** . -4. **ERASE :** -5. **SIZE :** -6. **FIND AND COUNT :** -7. **SWAP :** +4. **FIND :** +5. **ERASE :** This function is used to erase one perticuler element or some elements in a range in the set. So, There are **3** types of the erase function. I am includeing the codes for the **C++11**. [To see the codes for C++98 click on this link.](http://www.cplusplus.com/reference/set/set/erase/). Lets continue with the codes for C++11 and above : + - **Using constant value :** You can erase a constant value from the set using **`erase (const value_type& val);`** See the code below for better understanding : + ```cpp + #include + #include + + using namespace std; + + int main(){ + + set < int > MySet; // Declaring MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 23 , 45 + + MySet.erase(23); // This will erase 23 from the set + + cout << "The Elements of the set are : " << endl; + + for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one + { + // Here an_element is a member of MySet + cout << an_element << " "; + } + + cout << endl ; + + return 0; + } + ``` + This is how you can erase a constant value from the set. + - using iterator + - Erasing a range of number by iterator +6. **SIZE :** +7. **FIND AND COUNT :** +8. **SWAP :** 8. **UPPER AND LOWER :** From b13da55dcaef0782ecd6fc9171489f4814a7087d Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Tue, 30 Jun 2020 19:00:19 +0600 Subject: [PATCH 55/97] fixed set.md Added output --- STL/set.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/STL/set.md b/STL/set.md index 4b9d150..7247de9 100644 --- a/STL/set.md +++ b/STL/set.md @@ -166,6 +166,11 @@ The often used operations in set are : return 0; } ``` + **OUTPUT :** + ``` + The Elements of the set are : + 11 45 + ``` This is how you can erase a constant value from the set. - using iterator - Erasing a range of number by iterator From 2068fa18b1e9d6b9d745ef1314aa356e3d5d408e Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Wed, 1 Jul 2020 09:53:20 +0600 Subject: [PATCH 56/97] Update vector.md --- STL/vector.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index a2b1de3..9ed456c 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -307,12 +307,12 @@ We can declare a vector in several ways. Let us see some of them at a glance and ### Important Iterator Functions -| Function | Work of the function | -|--------------|-------------------------------------------------------------------------------------------------------------------------| -| **begin()** | Returns an iterator pointing to the first element of the vector | -| **end()** | Returns an iterator pointing to the theoretical element that follows the last element in a vector | -| **rbegin()** | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | -| **rend()** | Returns a reverse iterator pointing to the first element in the vector (reverse end). | +| Function | Work of the function | +|--------------------------------|-------------------------------------------------------------------------------------------------------------------------| +| **begin()**
v.begin() | Returns an iterator pointing to the first element of the vector | +| **end()**
v.end() | Returns an iterator pointing to the theoretical element that follows the last element in a vector | +| **rbegin()**
v.rbegin() | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | +| **rend()**
v.rend() | Returns a reverse iterator pointing to the first element in the vector (reverse end). | *** From 5768622e0ecb7a3025ed75ae2e6683695657a612 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Wed, 1 Jul 2020 09:57:26 +0600 Subject: [PATCH 57/97] Update vector.md --- STL/vector.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 9ed456c..826baae 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -307,12 +307,12 @@ We can declare a vector in several ways. Let us see some of them at a glance and ### Important Iterator Functions -| Function | Work of the function | -|--------------------------------|-------------------------------------------------------------------------------------------------------------------------| -| **begin()**
v.begin() | Returns an iterator pointing to the first element of the vector | -| **end()**
v.end() | Returns an iterator pointing to the theoretical element that follows the last element in a vector | -| **rbegin()**
v.rbegin() | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | -| **rend()**
v.rend() | Returns a reverse iterator pointing to the first element in the vector (reverse end). | +| Function | Work of the function | +|----------------------------------|-------------------------------------------------------------------------------------------------------------------------| +| **begin()**
`v.begin()` | Returns an iterator pointing to the first element of the vector | +| **end()**
`v.end()` | Returns an iterator pointing to the theoretical element that follows the last element in a vector | +| **rbegin()**
`v.rbegin()` | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | +| **rend()**
`v.rend()` | Returns a reverse iterator pointing to the first element in the vector (reverse end). | *** From daeeb6d1bb333fc6838995521a8e8ac9b49a1ccd Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Wed, 1 Jul 2020 10:24:39 +0600 Subject: [PATCH 58/97] Update vector.md --- STL/vector.md | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/STL/vector.md b/STL/vector.md index 826baae..14ddb45 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -318,41 +318,41 @@ We can declare a vector in several ways. Let us see some of them at a glance and ### Important Capacity Functions -| Function | Work of the function | -|---------------------|-----------------------------------------------------------------------------------------------------| -| **size()** | Returns the number of elements in the vector | -| **max_size()** | Returns the maximum number of elements that the vetor can hold | -| **capacity()** | Returns the size of the storage space currently allocated to the vector expressed as number | -| **resize(n)** | Resizes the container so that it can contain `'n'` elements | -| **empty()** | Returns whether the container is empty | -| **shrink_to_fit()** | Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity | -| **reserve(n)** | Requests that the vector capacity be at least enough to contain `'n'` elements | +| Function | Work of the function | +|------------------------------------------------|-----------------------------------------------------------------------------------------------------| +| **size()**
`v.size()` | Returns the number of elements in the vector | +| **max_size()**
`v.max_size()` | Returns the maximum number of elements that the vetor can hold | +| **capacity()**
`v.capacity` | Returns the size of the storage space currently allocated to the vector expressed as number | +| **resize()**
`v.resize(n)` | Resizes the container so that it can contain `'n'` elements | +| **empty()**
v.empty() | Returns whether the container is empty | +| **shrink_to_fit()**
`v.shrink_to_fit()` | Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity | +| **reserve()**
`v.reserve(n)` | Requests that the vector capacity be at least enough to contain `'n'` elements | *** ### Element Access Functions -| Function | Work of the function | -|----------------------------|--------------------------------------------------------------------------------------------------------| -| **reference operator [g]** | Returns a reference to the element at position `'g'` in the vector | -| **at()** | Returns a reference to the element at position `'g'` in the vector | -| **front()** | Returns a reference to the first element in the vector | -| **beck()** | Returns a reference to the last element of the vector | -| **data()** | Returns a direct pointer to the memory array used internally by the vector to store its owned elements | +| Function | Work of the function | +|------------------------------------------|--------------------------------------------------------------------------------------------------------| +| **reference operator [g]**
`v[g]` | Returns a reference to the element at position `'g'` in the vector | +| **at(g)**
`v.at(g)` | Returns a reference to the element at position `'g'` in the vector | +| **front()**
`v.front()` | Returns a reference to the first element in the vector | +| **back()**
`v.back()` | Returns a reference to the last element of the vector | +| **data()**
`v.data()` | Returns a direct pointer to the memory array used internally by the vector to store its owned elements | *** ### Modifiers -| Function | Work of the function | -|-----------------|------------------------------------------------------------------------------------------------| -| **assign()** | It assigns new value to the vector elements by replacing old ones | -| **push_back()** | It pushes the elements into the vector form the back | -| **pop_back()** | It removes elements from a vector from the back | -| **insert()** | It inserts new elements before the element at specified position | -| **erase()** | It removes elements from the specified position or specified range | -| **swap()** | It is used to swap the content of one vector with another vector of same type, size may differ | -| **clear()** | It is used to remove all the elements of the vector container | +| Function | Work of the function | +|-------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------| +| **assign()**
`v.assign(n, x)` | It assigns new value to the vector elements by replacing old ones.
More precisely, the first n elements becomes x. | +| **push_back()**
`v.push_back(x)` | It pushes the elements (x) into the vector form the back | +| **pop_back()**
`v.pop_back()` | It removes elements from a vector from the back | +| **insert()**
`v.insert(iterator position, x)` | It inserts new elements before the element at specified position | +| **erase()**
`v.erase(iterator position)`
`v.erase(iterator first, iterator last)` | It removes elements from the specified position or specified range | +| **swap()**
`v1.swap(v2)` | It is used to swap the content of one vector with another vector of same type, size may differ | +| **clear()**
`v.clear()` | It is used to remove all the elements of the vector container | *** From f2d28fdc3dbb52534d23eeb73d6568ddc72da82b Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Wed, 1 Jul 2020 10:33:00 +0600 Subject: [PATCH 59/97] Update vector.md --- STL/vector.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/vector.md b/STL/vector.md index 14ddb45..474b69f 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -324,7 +324,7 @@ We can declare a vector in several ways. Let us see some of them at a glance and | **max_size()**
`v.max_size()` | Returns the maximum number of elements that the vetor can hold | | **capacity()**
`v.capacity` | Returns the size of the storage space currently allocated to the vector expressed as number | | **resize()**
`v.resize(n)` | Resizes the container so that it can contain `'n'` elements | -| **empty()**
v.empty() | Returns whether the container is empty | +| **empty()**
`v.empty()` | Returns whether the container is empty | | **shrink_to_fit()**
`v.shrink_to_fit()` | Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity | | **reserve()**
`v.reserve(n)` | Requests that the vector capacity be at least enough to contain `'n'` elements | From eba5f9fc2918d12565a86dce17719fa4c5161859 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Wed, 1 Jul 2020 22:22:01 +0600 Subject: [PATCH 60/97] update set.md added erase using iterator --- STL/set.md | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index 7247de9..ac4c343 100644 --- a/STL/set.md +++ b/STL/set.md @@ -172,7 +172,46 @@ The often used operations in set are : 11 45 ``` This is how you can erase a constant value from the set. - - using iterator + - **Single Element using iterator :** We can also erase an element from the set using iterators. To do that we need to declare an iterator and then pass that iterator in the erase function which will cause deletation of that perticuler element. Lets see the code for better understanding : + ```cpp + #include + #include + + using namespace std; + + int main(){ + + set < int > MySet; // Declaring MySet + set < int >::iterator it; // Declaring Iterator for MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 23 , 45 + + it = MySet.find(23); // This will get the pointer to 23 + MySet.erase(it); // This will delete the pointed element which is 23 + + cout << "The Elements of the set are : " << endl; + + for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one + { + // Here an_element is a member of MySet + cout << an_element << " "; + } + + cout << endl ; + + return 0; + } + ``` + **OUTPUT :** + ``` + The Elements of the set are : + 11 45 + ``` - Erasing a range of number by iterator 6. **SIZE :** 7. **FIND AND COUNT :** From 86676e1e30a4fb882d93f9070a25914baaa91cb0 Mon Sep 17 00:00:00 2001 From: Shawon Majid Date: Wed, 1 Jul 2020 22:27:21 +0600 Subject: [PATCH 61/97] Create map.md --- STL/map.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 STL/map.md diff --git a/STL/map.md b/STL/map.md new file mode 100644 index 0000000..45e0896 --- /dev/null +++ b/STL/map.md @@ -0,0 +1,19 @@ +# Map in C++ + +## Introduction + +Map is one of the most powerful tools in STL. There are countless uses of maps and all of them are very useful. According to [cplusplus.com](http://www.cplusplus.com/reference/map/map/), Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. There are many things common between Set and Map. Infact some argues that **Map** is nothing but a 2D **Set**. So, without further due, lets jump into it! + +## Declaration +For using map you need to add the following header file: +```cpp +#include +``` +And the general structure of map is: +``` +map M; +``` +So, in a map, There are some elements which has a key value and a mapped value. ```No two mapped values can have same key values. key values are unique & and Key values are also sorted like set. but mapped values can be duplicate.``` For example lets declare a map of 'string as key' and 'integers as mapped value': +```cpp +map M; +``` From f2fb9b3ace6e048405075bc3f86c1793b375f63e Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Thu, 2 Jul 2020 02:24:31 +0600 Subject: [PATCH 62/97] Create func-codes.md --- STL/vector/func-codes.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 STL/vector/func-codes.md diff --git a/STL/vector/func-codes.md b/STL/vector/func-codes.md new file mode 100644 index 0000000..393af4b --- /dev/null +++ b/STL/vector/func-codes.md @@ -0,0 +1 @@ +This is a new page created by `Ariful Islam Shanto` From bfa233421a77cfa339378d601f1e103d2b1f4e12 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Thu, 2 Jul 2020 02:32:25 +0600 Subject: [PATCH 63/97] Update func-codes.md --- STL/vector/func-codes.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/STL/vector/func-codes.md b/STL/vector/func-codes.md index 393af4b..2ccdc27 100644 --- a/STL/vector/func-codes.md +++ b/STL/vector/func-codes.md @@ -1 +1,7 @@ This is a new page created by `Ariful Islam Shanto` + +*** + +# Some Useful Codes To Understands The Funcitons + +//will add later... From 12673cdd68c74b3aade99a2512c469fd0ab5f404 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Thu, 2 Jul 2020 17:59:09 +0600 Subject: [PATCH 64/97] update set.md Added ranged erase using iterator --- STL/set.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index ac4c343..bf0e556 100644 --- a/STL/set.md +++ b/STL/set.md @@ -212,7 +212,38 @@ The often used operations in set are : The Elements of the set are : 11 45 ``` - - Erasing a range of number by iterator + - **Erasing a range of number by iterator :** You can even erase a range of numbers from set using iterator. To do that you need to pass two iterator. One is the starting iterator and another one is the ending iterator. This two iterator tells erase function the starting and ending position of the erase operation. Lets see the code for implementation. + ```cpp + set < int > MySet; // Declaring MySet + set < int >::iterator it1, it2; + // Declaring Iterator for starting and ending position of erase function + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(17); // Inserting 17 + MySet.insert(94); // Inserting 94 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 17 , 23 , 45 , 94 + + it1 = MySet.find(17); // This will get the starting pointer to 17 + it2 = MySet.find(45); // This will get the ending pointer to 45 + it2++; // always remember to do 1 element increment for the ending iterator + //because it will remove all elements before it2 not the one that is in it2 + + MySet.erase(it1,it2); // This will delete 17 23 45 from the set + + cout << "The Elements of the set are : " << endl; + + for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one + { + // Here an_element is a member of MySet + cout << an_element << " "; + } + + cout << endl ; + ``` 6. **SIZE :** 7. **FIND AND COUNT :** 8. **SWAP :** From c332880b5de3ac48cae87d527a3e6dcd1a954f53 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:00:14 +0600 Subject: [PATCH 65/97] update set Added output --- STL/set.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/STL/set.md b/STL/set.md index bf0e556..3fce6e0 100644 --- a/STL/set.md +++ b/STL/set.md @@ -244,6 +244,11 @@ The often used operations in set are : cout << endl ; ``` + **OUTPUT :** + ``` + The Elements of the set are : + 11 94 + ``` 6. **SIZE :** 7. **FIND AND COUNT :** 8. **SWAP :** From e04cbe10fce9c32fb109d8c2246e5f59629dc578 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:16:59 +0600 Subject: [PATCH 66/97] update set.md Added size function --- STL/set.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index 3fce6e0..fc427ac 100644 --- a/STL/set.md +++ b/STL/set.md @@ -249,7 +249,25 @@ The often used operations in set are : The Elements of the set are : 11 94 ``` -6. **SIZE :** +6. **SIZE :** This is very basic function for all the datatypes and containers in STL. It will return how many elements are there in the set. Lets see the code for the implementation : + ```cpp + set < int > MySet; // Declaring MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(17); // Inserting 17 + MySet.insert(94); // Inserting 94 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 17 , 23 , 45 , 94 + + cout << "The size of MySet is :: " << MySet.size() << endl; + ``` + **OUTPUT :** + ``` + The size of MySet is :: 5 + ``` 7. **FIND AND COUNT :** 8. **SWAP :** 8. **UPPER AND LOWER :** From 651ca3350639be11a3910324f02ac11ea233b228 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:40:29 +0600 Subject: [PATCH 67/97] Update set.md Added std::count functiuon in the set --- STL/set.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index fc427ac..2413b0a 100644 --- a/STL/set.md +++ b/STL/set.md @@ -268,6 +268,24 @@ The often used operations in set are : ``` The size of MySet is :: 5 ``` -7. **FIND AND COUNT :** +7. **COUNT :** This returns the number of occurance of a perticuler element in a set. As we know default set does not allows the occurance of the same element more than once, so the count will return 1 or 0 only. But in case of _**multiset**_ ( which we will discuss later) count will return how many times you insert a perticuler element into the set. Lets now see the implementation of std::count() in the standard set : + ```cpp + set < int > MySet; // Declaring MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(17); // Inserting 17 + MySet.insert(94); // Inserting 94 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 17 , 23 , 45 , 94 + + cout << "45 is present in the set " << MySet.count(45) << " times" << endl; + ``` + **OUTPUT :** + ``` + 45 is present in the set 1 times + ``` 8. **SWAP :** 8. **UPPER AND LOWER :** From f0db652b964ae2f76d8fa84951cbfd0624d4bdfb Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Thu, 2 Jul 2020 21:12:47 +0600 Subject: [PATCH 68/97] update set Added Swap and print function inside it --- STL/set.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index 2413b0a..fbd7c30 100644 --- a/STL/set.md +++ b/STL/set.md @@ -287,5 +287,61 @@ The often used operations in set are : ``` 45 is present in the set 1 times ``` -8. **SWAP :** +8. **SWAP :** Swap is a very simple function. It swaps the elements between two sets. Lets see the code to see the implementation : + ```cpp + //Function to print set + void print_set (set < int > temp){ + for( auto an_element : temp ) + // Taking elements from temp set to an_element one by one + { + // Here an_element is a member of temp set + cout << an_element << " "; + } + cout << endl; + } + + int main(){ + + set < int > setA, setB; // Declaring two sets + + setA.insert(1); // Inserting 1 + setA.insert(2); // Inserting 2 + setA.insert(3); // Inserting 3 + // So, Now the setB should be : 1 , 2 , 3 + + setB.insert(10); // Inserting 10 + setB.insert(20); // Inserting 20 + setB.insert(30); // Inserting 30 + setB.insert(40); // Inserting 40 + // So, Now the setB should be : 10 , 20 , 30 , 40 + + cout << "Before swapping --> \n"; + cout << "setA :: "; + print_set(setA); // Printing setA + + cout << "setB :: "; + print_set(setB); // Printing setB + + setA.swap(setB); // SWAPING between setA and setB + + cout << "\nAfter swapping --> \n"; + cout << "setA :: "; + print_set(setA); // Printing setA + + cout << "setB :: "; + print_set(setB); // Printing setB + + return 0; + } + ``` + **OUTPUT :** + ```cpp + Before swapping --> + setA :: 1 2 3 + setB :: 10 20 30 40 + + After swapping --> + setA :: 10 20 30 40 + setB :: 1 2 3 + ``` 8. **UPPER AND LOWER :** From 12e32dfbcf488aea788dbf73069d8aa3e6ba1100 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Thu, 2 Jul 2020 22:19:29 +0600 Subject: [PATCH 69/97] updated set.md added user-defined set printing function --- STL/set.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/STL/set.md b/STL/set.md index fbd7c30..f913fb9 100644 --- a/STL/set.md +++ b/STL/set.md @@ -131,6 +131,36 @@ The often used operations in set are : 11 23 45 ``` I prefer to use this format because it is simple and easy to use. **BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP** . + - **User-defined function to print a set :** Now we will write an user-defined function which will print a set on its own. As a perameter it will take the set (call by value). As it will sent a copy of the set, it won't do any change to the original set in the main function. Lets see the function's implementation: + ```cpp + void print_set (set < int > temp){ + for( auto an_element : temp ) + // Taking elements from temp set to an_element one by one + { + // Here an_element is a member of temp set + cout << an_element << " "; + } + cout << endl; + } + + int main(){ + + set < int > MySet; // Declaring MySet + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 23 , 45 + + cout << "The Elements of the set are : " << endl; + + print_set(MySet); // Printing MySet + + return 0; + } + ``` 4. **FIND :** 5. **ERASE :** This function is used to erase one perticuler element or some elements in a range in the set. So, There are **3** types of the erase function. I am includeing the codes for the **C++11**. [To see the codes for C++98 click on this link.](http://www.cplusplus.com/reference/set/set/erase/). Lets continue with the codes for C++11 and above : - **Using constant value :** You can erase a constant value from the set using **`erase (const value_type& val);`** See the code below for better understanding : From bb7d55df868b13d74eb11c3f1144f55951b6f840 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Thu, 2 Jul 2020 23:10:38 +0600 Subject: [PATCH 70/97] update set.md Added find, distance, end, begin iterator and functions --- STL/set.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index f913fb9..e2d2c86 100644 --- a/STL/set.md +++ b/STL/set.md @@ -161,7 +161,57 @@ The often used operations in set are : return 0; } ``` -4. **FIND :** +4. **FIND :** This function is used to search an element from the set. It returns an iterator which points to the element in the set. If the element is not present in the set it will return the ending positon (` which is called std::end() `) of the set. You can also find the distance of the element using `std::distance(start_iterator,ending_iterator)` function. So, Lets see the implementation of the concepts mentioned above : + ```cpp + void print_set (set < int > temp){ + for( auto an_element : temp ) + // Taking elements from temp set to an_element one by one + { + // Here an_element is a member of temp set + cout << an_element << " "; + } + cout << endl; + } + + int main(){ + + set < int > MySet; // Declaring MySet + set < int >::iterator it; // Declaring an iterator of integer set + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(17); // Inserting 17 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 17 , 23 , 45 + + cout << "The Elements of the set are : " << endl; + print_set(MySet); // Printing MySet + cout << endl; + + it = MySet.find(23); // it will now point to 23 in the set + cout << *it << " is present in index no: " << distance( MySet.begin() , it ) << endl; + // You can see the value of it by using *it (This is also known as dereferencing) + // Here distance functions returns the distance between two iterators + + it = MySet.find(25); // it will now point to the end of the set as it is absent in the set + if(it == MySet.end()) { + // As 25 is not present it will point to MySet.end() + cout << "25 is not present in the set" << endl; + } + + return 0; + } + ``` + **OUTPUT :** + ```cpp + The Elements of the set are : + 11 17 23 45 + + 23 is present in index no: 2 + 25 is not present in the set + ``` 5. **ERASE :** This function is used to erase one perticuler element or some elements in a range in the set. So, There are **3** types of the erase function. I am includeing the codes for the **C++11**. [To see the codes for C++98 click on this link.](http://www.cplusplus.com/reference/set/set/erase/). Lets continue with the codes for C++11 and above : - **Using constant value :** You can erase a constant value from the set using **`erase (const value_type& val);`** See the code below for better understanding : ```cpp From 0c9ffedabb9e979495646ca30c1cc657e10e05b8 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:23:10 +0600 Subject: [PATCH 71/97] update set.md - Added iterator - Added iterator table - Added implementation of above --- STL/set.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index e2d2c86..b32e8d9 100644 --- a/STL/set.md +++ b/STL/set.md @@ -53,7 +53,72 @@ The often used operations in set are : } ``` Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user. -2. **ITERATOR :** +2. **ITERATOR :** Iterators are used to point out an element in the set as well as any other container in STL. [You can check this link to know more about iterators](https://definecoder.github.io/STL/iterator). Iterators are very important to access any element in the set. Lets see the list of the iterators for set and the code: + | Function | Work of the function | + |:--------------------------------------:|:-------------------------------------------------------------:| + | begin()
**MySet.begin()** | returns iterator to the **begin** of the set | + | end()
**MySet.end()** | returns iterator to the **end** of the set | + | advance()
**MySet.advance(it,x)** | _increments_ the position of the **it** by **x** | + | prev()
**MySet.prev(it,x)** | returns _decrement_ of the position of the **it** by **x** | + | next()
**MySet.next(it,x)** | returns _increment_ of the position of the **it** by **x** | + **Implementation :** + ```cpp + #include + #include + #include + // remember to include the header of iterator when you will use these functions + + using namespace std; + + void print_set (set < int > temp){ + for( auto an_element : temp ) + // Taking elements from temp set to an_element one by one + { + // Here an_element is a member of temp set + cout << an_element << " "; + } + cout << endl; + } + + int main(){ + + set < int > MySet; // Declaring MySet + set < int >::iterator it; // Declaring an iterator of integer set + + MySet.insert(45); // Inserting 45 + MySet.insert(23); // Inserting 23 + MySet.insert(11); // Inserting 11 + MySet.insert(17); // Inserting 17 + MySet.insert(45); + // 45 is already in the set so nothing happens + // So, Now the set should be : 11 , 17 , 23 , 45 + + cout << "The Elements of the set are : " << endl; + print_set(MySet); // Printing MySet (will be disscussed below) + cout << endl; + + it = MySet.begin(); + cout << "value of MySet.begin() is : " << *it << endl; + + it = MySet.end(); + it--; // decrementing it because end pointer does not contain any value + cout << "value of [MySet.end() - 1] is : " << *it << endl; + + it = MySet.begin(); + advance(it,2); + cout << "value of advance(MySet.begin(),2) is : " << *it << endl; + + it = MySet.begin(); + it = next(it,3); + cout << "value of next(MySet.begin(),3) is : " << *it << endl; + + it = MySet.end(); + it = prev(it,4); + cout << "value of prev(MySet.end(),4) is : " << *it << endl; + + return 0; + } + ``` 3. **PRINTING A SET :** Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. ```cpp From cb2b3c2669c046628d365371f5195b8f79f430d3 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:26:27 +0600 Subject: [PATCH 72/97] updated list.md updated upper list --- STL/set.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index b32e8d9..8482647 100644 --- a/STL/set.md +++ b/STL/set.md @@ -24,9 +24,10 @@ The often used operations in set are : - Insert - Iterator - Printing a set +- Find - Erase - Size -- Find and Count +- Count - Swap - Upper and Lower Bound From b8c8fb4cd56784b4dadfa712717c880d97150e01 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:32:38 +0600 Subject: [PATCH 73/97] changer formatting for site changer formatting for site --- STL/set.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/STL/set.md b/STL/set.md index 8482647..34b064a 100644 --- a/STL/set.md +++ b/STL/set.md @@ -54,8 +54,8 @@ The often used operations in set are : } ``` Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user. -2. **ITERATOR :** Iterators are used to point out an element in the set as well as any other container in STL. [You can check this link to know more about iterators](https://definecoder.github.io/STL/iterator). Iterators are very important to access any element in the set. Lets see the list of the iterators for set and the code: - | Function | Work of the function | +2. **ITERATOR :** Iterators are used to point out an element in the set as well as any other container in STL. [You can check this link to know more about iterators](https://definecoder.github.io/STL/iterator). Iterators are very important to access any element in the set. Lets see the list of the iterators for set and the code: + | Function | Work of the function | |:--------------------------------------:|:-------------------------------------------------------------:| | begin()
**MySet.begin()** | returns iterator to the **begin** of the set | | end()
**MySet.end()** | returns iterator to the **end** of the set | @@ -121,7 +121,7 @@ The often used operations in set are : } ``` 3. **PRINTING A SET :** Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: - - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. + - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. ```cpp #include #include @@ -152,15 +152,15 @@ The often used operations in set are : cout << endl ; return 0; - } + } ``` **OUTPUT :** - ``` + ```cpp The Elements of the set are : 11 23 45 ``` So, You can print or access the elemets of a set using iterator. - - _**Special Format of C++11**_ : This mathod is very easy to use and I prefer to use this type. Because, almost all the datatypes or STL containers support this type of iteration or traversal through the set or any container. Lets see the code to see the implementation : + - _**Special Format of C++11**_ : This mathod is very easy to use and I prefer to use this type. Because, almost all the datatypes or STL containers support this type of iteration or traversal through the set or any container. Lets see the code to see the implementation : ```cpp #include #include @@ -190,13 +190,13 @@ The often used operations in set are : return 0; } - ``` - **OUTPUT :** - ``` + ``` + **OUTPUT :** + ```cpp The Elements of the set are : 11 23 45 - ``` - I prefer to use this format because it is simple and easy to use. **BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP** . + ``` + I prefer to use this format because it is simple and easy to use. **BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP** . - **User-defined function to print a set :** Now we will write an user-defined function which will print a set on its own. As a perameter it will take the set (call by value). As it will sent a copy of the set, it won't do any change to the original set in the main function. Lets see the function's implementation: ```cpp void print_set (set < int > temp){ From a6b09358fc890064e39771333d43a867a43ab5f1 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:38:40 +0600 Subject: [PATCH 74/97] Create test.html --- STL/test.html | 499 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 499 insertions(+) create mode 100644 STL/test.html diff --git a/STL/test.html b/STL/test.html new file mode 100644 index 0000000..626c5ab --- /dev/null +++ b/STL/test.html @@ -0,0 +1,499 @@ +

Set in C++

+

Introduction

+

Set is a container which has very special for its functionality. This have some variants which are also discussed here later on. The main features of default set is :

+
    +
  • It stores each element once and don’t allow repeated elements. Which means default set stores only distinct elements.
  • +
  • It keeps elements in sorted order. You can customize how the elements will be sorted. But By default it keeps the elements in ascending order.
  • +
  • You can use other variants of the set like multiset, unordered_set, unordered_multiset as you need.
  • +
+

Declaration:

+

First of all you need to add the header file of set to use any functionality of set. To do that you have to add the following header :

+
#include <set>
+
+

After adding the header you can declare your set using this code:

+
set < data_type > MySet;
+// For example you may declare a set of integers nemmed MyIntSet using :
+set < int > MyIntSet;
+
+

In the place of data_type you may use any data type like integer, double, charecter, string, pair etc.

+

Basic operations

+

The often used operations in set are :

+
    +
  • Insert
  • +
  • Iterator
  • +
  • Printing a set
  • +
  • Find
  • +
  • Erase
  • +
  • Size
  • +
  • Count
  • +
  • Swap
  • +
  • Upper and Lower Bound
  • +
+

Implementation of the basic operations in set

+
    +
  1. INSERT : Insertion is a very basic operation of set. Using this operation you can insert a new element into your set. But remember insersion will add a new element if that element was absent there before because set do not allows same element’s occurance twice. So lets see the implementation :
    #include <iostream> 
    +#include <set> 
    +
    +using namespace std; 
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 23 , 45
    +    
    +    return 0;
    +}
    +
    +Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user.
  2. +
  3. ITERATOR : Iterators are used to point out an element in the set as well as any other container in STL. You can check this link to know more about iterators. Iterators are very important to access any element in the set. Lets see the list of the iterators for set and the code: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    FunctionWork of the function
    begin() <br> MySet.begin()returns iterator to the begin of the set
    end() <br> MySet.end()returns iterator to the end of the set
    advance() <br> MySet.advance(it,x)increments the position of the it by x
    prev() <br> MySet.prev(it,x)returns decrement of the position of the it by x
    next() <br> MySet.next(it,x)returns increment of the position of the it by x
    +Implementation :
    #include <iostream> 
    +#include <set> 
    +#include <iterator> 
    +// remember to include the header of iterator when you will use these functions
    +
    +using namespace std; 
    +
    +void print_set (set < int > temp){
    +for( auto an_element : temp ) 
    +// Taking elements from temp set to an_element one by one
    +{
    +    // Here an_element is a member of temp set
    +    cout << an_element << " ";
    +} 
    +cout << endl;
    +}
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +    set < int >::iterator it; // Declaring an iterator of integer set
    +
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(17); // Inserting 17
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 17 , 23 , 45
    +    
    +    cout << "The Elements of the set are : " << endl;
    +    print_set(MySet); // Printing MySet (will be disscussed below)
    +    cout << endl;
    +
    +    it = MySet.begin();
    +    cout << "value of MySet.begin() is : " << *it << endl;
    +    
    +    it = MySet.end();
    +    it--; // decrementing it because end pointer does not contain any value
    +    cout << "value of [MySet.end() - 1] is : " << *it << endl;
    +
    +    it = MySet.begin();
    +    advance(it,2);
    +    cout << "value of advance(MySet.begin(),2) is : " << *it << endl;
    +
    +    it = MySet.begin();
    +    it = next(it,3);
    +    cout << "value of next(MySet.begin(),3) is : " << *it << endl;
    +
    +    it = MySet.end();
    +    it = prev(it,4);
    +    cout << "value of prev(MySet.end(),4) is : " << *it << endl;
    +
    +    return 0;
    +}
    +
    +
  4. +
  5. PRINTING A SET : Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: +
      +
    • Using Iterator : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding.
    • +
    +
    #include <iostream> 
    +#include <set> 
    +
    +using namespace std; 
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +    set < int >::iterator it; 
    +    // Declaring an iterator to point to the elements of MySet
    +
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 23 , 45
    +
    +    cout << "The Elements of the set are : " << endl;
    +
    +    for( it=MySet.begin() ; it != MySet.end() ; it++ )
    +    {
    +        // Dereferencing the pointer( Iterator ) to see the value
    +        cout << *it << " ";
    +    } 
    +
    +    cout << endl ;
    +
    +    return 0;
    +}
    +
    +OUTPUT :
    The Elements of the set are : 
    +11 23 45 
    +
    +So, You can print or access the elemets of a set using iterator. +
      +
    • Special Format of C++11 : This mathod is very easy to use and I prefer to use this type. Because, almost all the datatypes or STL containers support this type of iteration or traversal through the set or any container. Lets see the code to see the implementation :
    • +
    +
    #include <iostream> 
    +#include <set> 
    +
    +using namespace std; 
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 23 , 45
    +
    +    cout << "The Elements of the set are : " << endl;
    +
    +    for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    +    {
    +        // Here an_element is a member of MySet
    +        cout << an_element << " ";
    +    } 
    +
    +    cout << endl ;
    +
    +    return 0;
    +}
    +
    +OUTPUT :
    The Elements of the set are : 
    +11 23 45 
    +
    +I prefer to use this format because it is simple and easy to use. BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP . +
      +
    • User-defined function to print a set : Now we will write an user-defined function which will print a set on its own. As a perameter it will take the set (call by value). As it will sent a copy of the set, it won’t do any change to the original set in the main function. Lets see the function’s implementation:
    • +
    +
    void print_set (set < int > temp){
    +for( auto an_element : temp ) 
    +// Taking elements from temp set to an_element one by one
    +{
    +    // Here an_element is a member of temp set
    +    cout << an_element << " ";
    +} 
    +cout << endl;
    +}
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +    
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 23 , 45
    +    
    +    cout << "The Elements of the set are : " << endl;
    +
    +    print_set(MySet); // Printing MySet
    +
    +    return 0;
    +}
    +
    +
  6. +
  7. FIND : This function is used to search an element from the set. It returns an iterator which points to the element in the set. If the element is not present in the set it will return the ending positon (which is called std::end()) of the set. You can also find the distance of the element using std::distance(start_iterator,ending_iterator) function. So, Lets see the implementation of the concepts mentioned above :
    void print_set (set < int > temp){
    +for( auto an_element : temp ) 
    +// Taking elements from temp set to an_element one by one
    +{
    +    // Here an_element is a member of temp set
    +    cout << an_element << " ";
    +} 
    +cout << endl;
    +}
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +    set < int >::iterator it; // Declaring an iterator of integer set
    +
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(17); // Inserting 17
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 17 , 23 , 45
    +    
    +    cout << "The Elements of the set are : " << endl;
    +    print_set(MySet); // Printing MySet
    +    cout << endl;
    +
    +    it = MySet.find(23); // it will now point to 23 in the set
    +    cout << *it << " is present in index no: " << distance( MySet.begin() , it ) << endl;
    +    // You can see the value of it by using *it (This is also known as dereferencing)
    +    // Here distance functions returns the distance between two iterators 
    +    
    +    it = MySet.find(25); // it will now point to the end of the set as it is absent in the set
    +    if(it == MySet.end()) {
    +        // As 25 is not present it will point to MySet.end()
    +        cout << "25 is not present in the set" << endl;
    +    }
    +
    +    return 0;
    +}
    +
    +OUTPUT :
    The Elements of the set are : 
    +11 17 23 45 
    +
    +23 is present in index no: 2
    +25 is not present in the set
    +
    +
  8. +
  9. ERASE : This function is used to erase one perticuler element or some elements in a range in the set. So, There are 3 types of the erase function. I am includeing the codes for the C++11. To see the codes for C++98 click on this link.. Lets continue with the codes for C++11 and above : +
      +
    • Using constant value : You can erase a constant value from the set using erase (const value_type& val); See the code below for better understanding :
    • +
    +
    #include <iostream> 
    +#include <set> 
    +
    +using namespace std; 
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 23 , 45
    +
    +    MySet.erase(23); // This will erase 23 from the set
    +
    +    cout << "The Elements of the set are : " << endl;
    +
    +    for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    +    {
    +        // Here an_element is a member of MySet
    +        cout << an_element << " ";
    +    } 
    +
    +    cout << endl ;
    +
    +    return 0;
    +}
    +
    +OUTPUT :
    The Elements of the set are : 
    +11 45 
    +
    +This is how you can erase a constant value from the set. +
      +
    • Single Element using iterator : We can also erase an element from the set using iterators. To do that we need to declare an iterator and then pass that iterator in the erase function which will cause deletation of that perticuler element. Lets see the code for better understanding :
    • +
    +
    #include <iostream> 
    +#include <set> 
    +
    +using namespace std; 
    +
    +int main(){
    +
    +    set < int > MySet; // Declaring MySet
    +    set < int >::iterator it; // Declaring Iterator for MySet
    +
    +    MySet.insert(45); // Inserting 45
    +    MySet.insert(23); // Inserting 23
    +    MySet.insert(11); // Inserting 11
    +    MySet.insert(45); 
    +    // 45 is already in the set so nothing happens
    +    // So, Now the set should be : 11 , 23 , 45
    +
    +    it = MySet.find(23); // This will get the pointer to 23
    +    MySet.erase(it); // This will delete the pointed element which is 23
    +
    +    cout << "The Elements of the set are : " << endl;
    +
    +    for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    +    {
    +        // Here an_element is a member of MySet
    +        cout << an_element << " ";
    +    } 
    +
    +    cout << endl ;
    +
    +    return 0;
    +}
    +
    +OUTPUT :
    The Elements of the set are : 
    +11 45 
    +
    +
      +
    • Erasing a range of number by iterator : You can even erase a range of numbers from set using iterator. To do that you need to pass two iterator. One is the starting iterator and another one is the ending iterator. This two iterator tells erase function the starting and ending position of the erase operation. Lets see the code for implementation.
    • +
    +
    set < int > MySet; // Declaring MySet
    +set < int >::iterator it1, it2; 
    +// Declaring Iterator for starting and ending position of erase function
    +
    +MySet.insert(45); // Inserting 45
    +MySet.insert(23); // Inserting 23
    +MySet.insert(11); // Inserting 11
    +MySet.insert(17); // Inserting 17
    +MySet.insert(94); // Inserting 94
    +MySet.insert(45); 
    +// 45 is already in the set so nothing happens
    +// So, Now the set should be : 11 , 17 , 23 , 45 , 94
    +
    +it1 = MySet.find(17); // This will get the starting pointer to 17
    +it2 = MySet.find(45); // This will get the ending pointer to 45
    +it2++; // always remember to do 1 element increment for the ending iterator
    +//because it will remove all elements before it2 not the one that is in it2
    +
    +MySet.erase(it1,it2); // This will delete 17 23 45 from the set
    +
    +cout << "The Elements of the set are : " << endl;
    +
    +for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    +{
    +    // Here an_element is a member of MySet
    +    cout << an_element << " ";
    +} 
    +
    +cout << endl ;
    +
    +OUTPUT :
    The Elements of the set are : 
    +11 94 
    +
    +
  10. +
  11. SIZE : This is very basic function for all the datatypes and containers in STL. It will return how many elements are there in the set. Lets see the code for the implementation :
    set < int > MySet; // Declaring MySet
    +
    +MySet.insert(45); // Inserting 45
    +MySet.insert(23); // Inserting 23
    +MySet.insert(11); // Inserting 11
    +MySet.insert(17); // Inserting 17
    +MySet.insert(94); // Inserting 94
    +MySet.insert(45); 
    +// 45 is already in the set so nothing happens
    +// So, Now the set should be : 11 , 17 , 23 , 45 , 94
    +
    +cout << "The size of MySet is :: " << MySet.size() << endl;
    +
    +OUTPUT :
    The size of MySet is :: 5
    +
    +
  12. +
  13. COUNT : This returns the number of occurance of a perticuler element in a set. As we know default set does not allows the occurance of the same element more than once, so the count will return 1 or 0 only. But in case of multiset ( which we will discuss later) count will return how many times you insert a perticuler element into the set. Lets now see the implementation of std::count() in the standard set :
     set < int > MySet; // Declaring MySet
    + 
    + MySet.insert(45); // Inserting 45
    + MySet.insert(23); // Inserting 23
    + MySet.insert(11); // Inserting 11
    + MySet.insert(17); // Inserting 17
    + MySet.insert(94); // Inserting 94
    + MySet.insert(45); 
    + // 45 is already in the set so nothing happens
    + // So, Now the set should be : 11 , 17 , 23 , 45 , 94
    +
    + cout << "45 is present in the set " << MySet.count(45) << " times" << endl;
    +
    +OUTPUT :
    45 is present in the set 1 times
    +
    +
  14. +
  15. SWAP : Swap is a very simple function. It swaps the elements between two sets. Lets see the code to see the implementation :
    //Function to print set
    +void print_set (set < int > temp){
    +    for( auto an_element : temp ) 
    +    // Taking elements from temp set to an_element one by one
    +    {
    +        // Here an_element is a member of temp set
    +        cout << an_element << " ";
    +    } 
    +    cout << endl;
    +}
    +
    +int main(){
    +
    +    set < int > setA, setB; // Declaring two sets
    +    
    +    setA.insert(1); // Inserting 1
    +    setA.insert(2); // Inserting 2
    +    setA.insert(3); // Inserting 3
    +    // So, Now the setB should be : 1 , 2 , 3
    +
    +    setB.insert(10); // Inserting 10
    +    setB.insert(20); // Inserting 20
    +    setB.insert(30); // Inserting 30 
    +    setB.insert(40); // Inserting 40 
    +    // So, Now the setB should be : 10 , 20 , 30 , 40
    +
    +    cout << "Before swapping --> \n";
    +    cout << "setA :: ";
    +    print_set(setA); // Printing setA
    +    
    +    cout << "setB :: ";
    +    print_set(setB); // Printing setB
    +
    +    setA.swap(setB); // SWAPING between setA and setB
    +
    +    cout << "\nAfter swapping --> \n";
    +    cout << "setA :: ";
    +    print_set(setA); // Printing setA
    +    
    +    cout << "setB :: ";
    +    print_set(setB); // Printing setB
    +
    +    return 0;
    +}
    +
    +OUTPUT :
    Before swapping --> 
    +setA :: 1 2 3 
    +setB :: 10 20 30 40 
    +
    +After swapping --> 
    +setA :: 10 20 30 40 
    +setB :: 1 2 3 
    +
    +
  16. +
  17. UPPER AND LOWER :
  18. +
From 4b368df742dfe9fa6e1dcb298fdd1c907428a2af Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:39:35 +0600 Subject: [PATCH 75/97] Delete test.html --- STL/test.html | 499 -------------------------------------------------- 1 file changed, 499 deletions(-) delete mode 100644 STL/test.html diff --git a/STL/test.html b/STL/test.html deleted file mode 100644 index 626c5ab..0000000 --- a/STL/test.html +++ /dev/null @@ -1,499 +0,0 @@ -

Set in C++

-

Introduction

-

Set is a container which has very special for its functionality. This have some variants which are also discussed here later on. The main features of default set is :

-
    -
  • It stores each element once and don’t allow repeated elements. Which means default set stores only distinct elements.
  • -
  • It keeps elements in sorted order. You can customize how the elements will be sorted. But By default it keeps the elements in ascending order.
  • -
  • You can use other variants of the set like multiset, unordered_set, unordered_multiset as you need.
  • -
-

Declaration:

-

First of all you need to add the header file of set to use any functionality of set. To do that you have to add the following header :

-
#include <set>
-
-

After adding the header you can declare your set using this code:

-
set < data_type > MySet;
-// For example you may declare a set of integers nemmed MyIntSet using :
-set < int > MyIntSet;
-
-

In the place of data_type you may use any data type like integer, double, charecter, string, pair etc.

-

Basic operations

-

The often used operations in set are :

-
    -
  • Insert
  • -
  • Iterator
  • -
  • Printing a set
  • -
  • Find
  • -
  • Erase
  • -
  • Size
  • -
  • Count
  • -
  • Swap
  • -
  • Upper and Lower Bound
  • -
-

Implementation of the basic operations in set

-
    -
  1. INSERT : Insertion is a very basic operation of set. Using this operation you can insert a new element into your set. But remember insersion will add a new element if that element was absent there before because set do not allows same element’s occurance twice. So lets see the implementation :
    #include <iostream> 
    -#include <set> 
    -
    -using namespace std; 
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 23 , 45
    -    
    -    return 0;
    -}
    -
    -Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user.
  2. -
  3. ITERATOR : Iterators are used to point out an element in the set as well as any other container in STL. You can check this link to know more about iterators. Iterators are very important to access any element in the set. Lets see the list of the iterators for set and the code: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    FunctionWork of the function
    begin() <br> MySet.begin()returns iterator to the begin of the set
    end() <br> MySet.end()returns iterator to the end of the set
    advance() <br> MySet.advance(it,x)increments the position of the it by x
    prev() <br> MySet.prev(it,x)returns decrement of the position of the it by x
    next() <br> MySet.next(it,x)returns increment of the position of the it by x
    -Implementation :
    #include <iostream> 
    -#include <set> 
    -#include <iterator> 
    -// remember to include the header of iterator when you will use these functions
    -
    -using namespace std; 
    -
    -void print_set (set < int > temp){
    -for( auto an_element : temp ) 
    -// Taking elements from temp set to an_element one by one
    -{
    -    // Here an_element is a member of temp set
    -    cout << an_element << " ";
    -} 
    -cout << endl;
    -}
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -    set < int >::iterator it; // Declaring an iterator of integer set
    -
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(17); // Inserting 17
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 17 , 23 , 45
    -    
    -    cout << "The Elements of the set are : " << endl;
    -    print_set(MySet); // Printing MySet (will be disscussed below)
    -    cout << endl;
    -
    -    it = MySet.begin();
    -    cout << "value of MySet.begin() is : " << *it << endl;
    -    
    -    it = MySet.end();
    -    it--; // decrementing it because end pointer does not contain any value
    -    cout << "value of [MySet.end() - 1] is : " << *it << endl;
    -
    -    it = MySet.begin();
    -    advance(it,2);
    -    cout << "value of advance(MySet.begin(),2) is : " << *it << endl;
    -
    -    it = MySet.begin();
    -    it = next(it,3);
    -    cout << "value of next(MySet.begin(),3) is : " << *it << endl;
    -
    -    it = MySet.end();
    -    it = prev(it,4);
    -    cout << "value of prev(MySet.end(),4) is : " << *it << endl;
    -
    -    return 0;
    -}
    -
    -
  4. -
  5. PRINTING A SET : Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: -
      -
    • Using Iterator : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding.
    • -
    -
    #include <iostream> 
    -#include <set> 
    -
    -using namespace std; 
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -    set < int >::iterator it; 
    -    // Declaring an iterator to point to the elements of MySet
    -
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 23 , 45
    -
    -    cout << "The Elements of the set are : " << endl;
    -
    -    for( it=MySet.begin() ; it != MySet.end() ; it++ )
    -    {
    -        // Dereferencing the pointer( Iterator ) to see the value
    -        cout << *it << " ";
    -    } 
    -
    -    cout << endl ;
    -
    -    return 0;
    -}
    -
    -OUTPUT :
    The Elements of the set are : 
    -11 23 45 
    -
    -So, You can print or access the elemets of a set using iterator. -
      -
    • Special Format of C++11 : This mathod is very easy to use and I prefer to use this type. Because, almost all the datatypes or STL containers support this type of iteration or traversal through the set or any container. Lets see the code to see the implementation :
    • -
    -
    #include <iostream> 
    -#include <set> 
    -
    -using namespace std; 
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 23 , 45
    -
    -    cout << "The Elements of the set are : " << endl;
    -
    -    for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    -    {
    -        // Here an_element is a member of MySet
    -        cout << an_element << " ";
    -    } 
    -
    -    cout << endl ;
    -
    -    return 0;
    -}
    -
    -OUTPUT :
    The Elements of the set are : 
    -11 23 45 
    -
    -I prefer to use this format because it is simple and easy to use. BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP . -
      -
    • User-defined function to print a set : Now we will write an user-defined function which will print a set on its own. As a perameter it will take the set (call by value). As it will sent a copy of the set, it won’t do any change to the original set in the main function. Lets see the function’s implementation:
    • -
    -
    void print_set (set < int > temp){
    -for( auto an_element : temp ) 
    -// Taking elements from temp set to an_element one by one
    -{
    -    // Here an_element is a member of temp set
    -    cout << an_element << " ";
    -} 
    -cout << endl;
    -}
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -    
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 23 , 45
    -    
    -    cout << "The Elements of the set are : " << endl;
    -
    -    print_set(MySet); // Printing MySet
    -
    -    return 0;
    -}
    -
    -
  6. -
  7. FIND : This function is used to search an element from the set. It returns an iterator which points to the element in the set. If the element is not present in the set it will return the ending positon (which is called std::end()) of the set. You can also find the distance of the element using std::distance(start_iterator,ending_iterator) function. So, Lets see the implementation of the concepts mentioned above :
    void print_set (set < int > temp){
    -for( auto an_element : temp ) 
    -// Taking elements from temp set to an_element one by one
    -{
    -    // Here an_element is a member of temp set
    -    cout << an_element << " ";
    -} 
    -cout << endl;
    -}
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -    set < int >::iterator it; // Declaring an iterator of integer set
    -
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(17); // Inserting 17
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 17 , 23 , 45
    -    
    -    cout << "The Elements of the set are : " << endl;
    -    print_set(MySet); // Printing MySet
    -    cout << endl;
    -
    -    it = MySet.find(23); // it will now point to 23 in the set
    -    cout << *it << " is present in index no: " << distance( MySet.begin() , it ) << endl;
    -    // You can see the value of it by using *it (This is also known as dereferencing)
    -    // Here distance functions returns the distance between two iterators 
    -    
    -    it = MySet.find(25); // it will now point to the end of the set as it is absent in the set
    -    if(it == MySet.end()) {
    -        // As 25 is not present it will point to MySet.end()
    -        cout << "25 is not present in the set" << endl;
    -    }
    -
    -    return 0;
    -}
    -
    -OUTPUT :
    The Elements of the set are : 
    -11 17 23 45 
    -
    -23 is present in index no: 2
    -25 is not present in the set
    -
    -
  8. -
  9. ERASE : This function is used to erase one perticuler element or some elements in a range in the set. So, There are 3 types of the erase function. I am includeing the codes for the C++11. To see the codes for C++98 click on this link.. Lets continue with the codes for C++11 and above : -
      -
    • Using constant value : You can erase a constant value from the set using erase (const value_type& val); See the code below for better understanding :
    • -
    -
    #include <iostream> 
    -#include <set> 
    -
    -using namespace std; 
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 23 , 45
    -
    -    MySet.erase(23); // This will erase 23 from the set
    -
    -    cout << "The Elements of the set are : " << endl;
    -
    -    for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    -    {
    -        // Here an_element is a member of MySet
    -        cout << an_element << " ";
    -    } 
    -
    -    cout << endl ;
    -
    -    return 0;
    -}
    -
    -OUTPUT :
    The Elements of the set are : 
    -11 45 
    -
    -This is how you can erase a constant value from the set. -
      -
    • Single Element using iterator : We can also erase an element from the set using iterators. To do that we need to declare an iterator and then pass that iterator in the erase function which will cause deletation of that perticuler element. Lets see the code for better understanding :
    • -
    -
    #include <iostream> 
    -#include <set> 
    -
    -using namespace std; 
    -
    -int main(){
    -
    -    set < int > MySet; // Declaring MySet
    -    set < int >::iterator it; // Declaring Iterator for MySet
    -
    -    MySet.insert(45); // Inserting 45
    -    MySet.insert(23); // Inserting 23
    -    MySet.insert(11); // Inserting 11
    -    MySet.insert(45); 
    -    // 45 is already in the set so nothing happens
    -    // So, Now the set should be : 11 , 23 , 45
    -
    -    it = MySet.find(23); // This will get the pointer to 23
    -    MySet.erase(it); // This will delete the pointed element which is 23
    -
    -    cout << "The Elements of the set are : " << endl;
    -
    -    for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    -    {
    -        // Here an_element is a member of MySet
    -        cout << an_element << " ";
    -    } 
    -
    -    cout << endl ;
    -
    -    return 0;
    -}
    -
    -OUTPUT :
    The Elements of the set are : 
    -11 45 
    -
    -
      -
    • Erasing a range of number by iterator : You can even erase a range of numbers from set using iterator. To do that you need to pass two iterator. One is the starting iterator and another one is the ending iterator. This two iterator tells erase function the starting and ending position of the erase operation. Lets see the code for implementation.
    • -
    -
    set < int > MySet; // Declaring MySet
    -set < int >::iterator it1, it2; 
    -// Declaring Iterator for starting and ending position of erase function
    -
    -MySet.insert(45); // Inserting 45
    -MySet.insert(23); // Inserting 23
    -MySet.insert(11); // Inserting 11
    -MySet.insert(17); // Inserting 17
    -MySet.insert(94); // Inserting 94
    -MySet.insert(45); 
    -// 45 is already in the set so nothing happens
    -// So, Now the set should be : 11 , 17 , 23 , 45 , 94
    -
    -it1 = MySet.find(17); // This will get the starting pointer to 17
    -it2 = MySet.find(45); // This will get the ending pointer to 45
    -it2++; // always remember to do 1 element increment for the ending iterator
    -//because it will remove all elements before it2 not the one that is in it2
    -
    -MySet.erase(it1,it2); // This will delete 17 23 45 from the set
    -
    -cout << "The Elements of the set are : " << endl;
    -
    -for( auto an_element : MySet ) // Taking elements from MySet to an_element ome by one
    -{
    -    // Here an_element is a member of MySet
    -    cout << an_element << " ";
    -} 
    -
    -cout << endl ;
    -
    -OUTPUT :
    The Elements of the set are : 
    -11 94 
    -
    -
  10. -
  11. SIZE : This is very basic function for all the datatypes and containers in STL. It will return how many elements are there in the set. Lets see the code for the implementation :
    set < int > MySet; // Declaring MySet
    -
    -MySet.insert(45); // Inserting 45
    -MySet.insert(23); // Inserting 23
    -MySet.insert(11); // Inserting 11
    -MySet.insert(17); // Inserting 17
    -MySet.insert(94); // Inserting 94
    -MySet.insert(45); 
    -// 45 is already in the set so nothing happens
    -// So, Now the set should be : 11 , 17 , 23 , 45 , 94
    -
    -cout << "The size of MySet is :: " << MySet.size() << endl;
    -
    -OUTPUT :
    The size of MySet is :: 5
    -
    -
  12. -
  13. COUNT : This returns the number of occurance of a perticuler element in a set. As we know default set does not allows the occurance of the same element more than once, so the count will return 1 or 0 only. But in case of multiset ( which we will discuss later) count will return how many times you insert a perticuler element into the set. Lets now see the implementation of std::count() in the standard set :
     set < int > MySet; // Declaring MySet
    - 
    - MySet.insert(45); // Inserting 45
    - MySet.insert(23); // Inserting 23
    - MySet.insert(11); // Inserting 11
    - MySet.insert(17); // Inserting 17
    - MySet.insert(94); // Inserting 94
    - MySet.insert(45); 
    - // 45 is already in the set so nothing happens
    - // So, Now the set should be : 11 , 17 , 23 , 45 , 94
    -
    - cout << "45 is present in the set " << MySet.count(45) << " times" << endl;
    -
    -OUTPUT :
    45 is present in the set 1 times
    -
    -
  14. -
  15. SWAP : Swap is a very simple function. It swaps the elements between two sets. Lets see the code to see the implementation :
    //Function to print set
    -void print_set (set < int > temp){
    -    for( auto an_element : temp ) 
    -    // Taking elements from temp set to an_element one by one
    -    {
    -        // Here an_element is a member of temp set
    -        cout << an_element << " ";
    -    } 
    -    cout << endl;
    -}
    -
    -int main(){
    -
    -    set < int > setA, setB; // Declaring two sets
    -    
    -    setA.insert(1); // Inserting 1
    -    setA.insert(2); // Inserting 2
    -    setA.insert(3); // Inserting 3
    -    // So, Now the setB should be : 1 , 2 , 3
    -
    -    setB.insert(10); // Inserting 10
    -    setB.insert(20); // Inserting 20
    -    setB.insert(30); // Inserting 30 
    -    setB.insert(40); // Inserting 40 
    -    // So, Now the setB should be : 10 , 20 , 30 , 40
    -
    -    cout << "Before swapping --> \n";
    -    cout << "setA :: ";
    -    print_set(setA); // Printing setA
    -    
    -    cout << "setB :: ";
    -    print_set(setB); // Printing setB
    -
    -    setA.swap(setB); // SWAPING between setA and setB
    -
    -    cout << "\nAfter swapping --> \n";
    -    cout << "setA :: ";
    -    print_set(setA); // Printing setA
    -    
    -    cout << "setB :: ";
    -    print_set(setB); // Printing setB
    -
    -    return 0;
    -}
    -
    -OUTPUT :
    Before swapping --> 
    -setA :: 1 2 3 
    -setB :: 10 20 30 40 
    -
    -After swapping --> 
    -setA :: 10 20 30 40 
    -setB :: 1 2 3 
    -
    -
  16. -
  17. UPPER AND LOWER :
  18. -
From b518a204040b0e28e3bfcc0657bb4b7135cccc5a Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:42:16 +0600 Subject: [PATCH 76/97] Update set.md --- STL/set.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index 34b064a..e16012c 100644 --- a/STL/set.md +++ b/STL/set.md @@ -33,6 +33,7 @@ The often used operations in set are : ## Implementation of the basic operations in set 1. **INSERT :** Insertion is a very basic operation of set. Using this operation you can insert a new element into your set. But remember insersion will add a new element if that element was absent there before because set do not allows same element's occurance twice. So lets see the implementation : + ```cpp #include #include @@ -55,14 +56,17 @@ The often used operations in set are : ``` Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user. 2. **ITERATOR :** Iterators are used to point out an element in the set as well as any other container in STL. [You can check this link to know more about iterators](https://definecoder.github.io/STL/iterator). Iterators are very important to access any element in the set. Lets see the list of the iterators for set and the code: + | Function | Work of the function | |:--------------------------------------:|:-------------------------------------------------------------:| | begin()
**MySet.begin()** | returns iterator to the **begin** of the set | | end()
**MySet.end()** | returns iterator to the **end** of the set | | advance()
**MySet.advance(it,x)** | _increments_ the position of the **it** by **x** | | prev()
**MySet.prev(it,x)** | returns _decrement_ of the position of the **it** by **x** | - | next()
**MySet.next(it,x)** | returns _increment_ of the position of the **it** by **x** | + | next()
**MySet.next(it,x)** | returns _increment_ of the position of the **it** by **x** | + **Implementation :** + ```cpp #include #include @@ -120,6 +124,7 @@ The often used operations in set are : return 0; } ``` + 3. **PRINTING A SET :** Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. ```cpp From f2559e0fa32057f6ae580eb74a8ef2da63611597 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:48:14 +0600 Subject: [PATCH 77/97] Changed formatting Fixed website rendering Syntex issues --- STL/set.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/STL/set.md b/STL/set.md index e16012c..28f7b9d 100644 --- a/STL/set.md +++ b/STL/set.md @@ -127,6 +127,7 @@ The often used operations in set are : 3. **PRINTING A SET :** Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. + ```cpp #include #include @@ -159,13 +160,17 @@ The often used operations in set are : return 0; } ``` + **OUTPUT :** + ```cpp The Elements of the set are : 11 23 45 ``` + So, You can print or access the elemets of a set using iterator. - _**Special Format of C++11**_ : This mathod is very easy to use and I prefer to use this type. Because, almost all the datatypes or STL containers support this type of iteration or traversal through the set or any container. Lets see the code to see the implementation : + ```cpp #include #include @@ -196,13 +201,17 @@ The often used operations in set are : return 0; } ``` + **OUTPUT :** + ```cpp The Elements of the set are : 11 23 45 ``` + I prefer to use this format because it is simple and easy to use. **BUT REMEMBER THIS CAN ONLY BE USED IN C++11 OR HIGHER VERSION OF CPP** . - **User-defined function to print a set :** Now we will write an user-defined function which will print a set on its own. As a perameter it will take the set (call by value). As it will sent a copy of the set, it won't do any change to the original set in the main function. Lets see the function's implementation: + ```cpp void print_set (set < int > temp){ for( auto an_element : temp ) @@ -232,7 +241,16 @@ The often used operations in set are : return 0; } ``` + + **OUTPUT :** + + ```cpp + The Elements of the set are : + 11 23 45 + ``` + 4. **FIND :** This function is used to search an element from the set. It returns an iterator which points to the element in the set. If the element is not present in the set it will return the ending positon (` which is called std::end() `) of the set. You can also find the distance of the element using `std::distance(start_iterator,ending_iterator)` function. So, Lets see the implementation of the concepts mentioned above : + ```cpp void print_set (set < int > temp){ for( auto an_element : temp ) @@ -275,7 +293,9 @@ The often used operations in set are : return 0; } ``` + **OUTPUT :** + ```cpp The Elements of the set are : 11 17 23 45 @@ -283,8 +303,10 @@ The often used operations in set are : 23 is present in index no: 2 25 is not present in the set ``` + 5. **ERASE :** This function is used to erase one perticuler element or some elements in a range in the set. So, There are **3** types of the erase function. I am includeing the codes for the **C++11**. [To see the codes for C++98 click on this link.](http://www.cplusplus.com/reference/set/set/erase/). Lets continue with the codes for C++11 and above : - **Using constant value :** You can erase a constant value from the set using **`erase (const value_type& val);`** See the code below for better understanding : + ```cpp #include #include @@ -317,13 +339,17 @@ The often used operations in set are : return 0; } ``` + **OUTPUT :** - ``` + + ```cpp The Elements of the set are : 11 45 ``` + This is how you can erase a constant value from the set. - **Single Element using iterator :** We can also erase an element from the set using iterators. To do that we need to declare an iterator and then pass that iterator in the erase function which will cause deletation of that perticuler element. Lets see the code for better understanding : + ```cpp #include #include @@ -358,12 +384,16 @@ The often used operations in set are : return 0; } ``` + **OUTPUT :** + ``` The Elements of the set are : 11 45 ``` + - **Erasing a range of number by iterator :** You can even erase a range of numbers from set using iterator. To do that you need to pass two iterator. One is the starting iterator and another one is the ending iterator. This two iterator tells erase function the starting and ending position of the erase operation. Lets see the code for implementation. + ```cpp set < int > MySet; // Declaring MySet set < int >::iterator it1, it2; @@ -395,12 +425,16 @@ The often used operations in set are : cout << endl ; ``` + **OUTPUT :** + ``` The Elements of the set are : 11 94 ``` + 6. **SIZE :** This is very basic function for all the datatypes and containers in STL. It will return how many elements are there in the set. Lets see the code for the implementation : + ```cpp set < int > MySet; // Declaring MySet @@ -415,11 +449,15 @@ The often used operations in set are : cout << "The size of MySet is :: " << MySet.size() << endl; ``` + **OUTPUT :** + ``` The size of MySet is :: 5 ``` + 7. **COUNT :** This returns the number of occurance of a perticuler element in a set. As we know default set does not allows the occurance of the same element more than once, so the count will return 1 or 0 only. But in case of _**multiset**_ ( which we will discuss later) count will return how many times you insert a perticuler element into the set. Lets now see the implementation of std::count() in the standard set : + ```cpp set < int > MySet; // Declaring MySet @@ -434,11 +472,15 @@ The often used operations in set are : cout << "45 is present in the set " << MySet.count(45) << " times" << endl; ``` - **OUTPUT :** - ``` + + **OUTPUT :** + + ``` 45 is present in the set 1 times ``` + 8. **SWAP :** Swap is a very simple function. It swaps the elements between two sets. Lets see the code to see the implementation : + ```cpp //Function to print set void print_set (set < int > temp){ @@ -485,7 +527,9 @@ The often used operations in set are : return 0; } ``` + **OUTPUT :** + ```cpp Before swapping --> setA :: 1 2 3 @@ -495,4 +539,5 @@ The often used operations in set are : setA :: 10 20 30 40 setB :: 1 2 3 ``` + 8. **UPPER AND LOWER :** From 6669fc595e06b0a8aa6bedd062368641445e5dd4 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Fri, 3 Jul 2020 02:58:24 +0600 Subject: [PATCH 78/97] updated set added output of iterator --- STL/set.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/STL/set.md b/STL/set.md index 28f7b9d..d91a390 100644 --- a/STL/set.md +++ b/STL/set.md @@ -125,6 +125,19 @@ The often used operations in set are : } ``` + **OUTPUT :** + + ```cpp + The Elements of the set are : + 11 17 23 45 + + value of MySet.begin() is : 11 + value of [MySet.end() - 1] is : 45 + value of advance(MySet.begin(),2) is : 23 + value of next(MySet.begin(),3) is : 45 + value of prev(MySet.end(),4) is : 11 + ``` + 3. **PRINTING A SET :** Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. From c0959c3244a7d34fd991931aa645796e3bc01c62 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sat, 4 Jul 2020 02:55:13 +0600 Subject: [PATCH 79/97] updated home added map link --- STL/home.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/home.md b/STL/home.md index 7909d76..4c1dbcb 100644 --- a/STL/home.md +++ b/STL/home.md @@ -16,7 +16,7 @@ STL is a very powefull tool for **competitive programming**. It saves a lot of t * Multiset * Unordered Set * Unordered Multiset -- Map +- [Map](https://definecoder.github.io/STL/map) * Multimap * Unordered Map * Unordered Multimap From 9acf30a3bbf05f05b367473fd2a7ec0d03704444 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Sat, 4 Jul 2020 04:34:31 +0600 Subject: [PATCH 80/97] Added upper and lower bound to the set,md Added upper and lower bound to the set,md almost in the end --- STL/set.md | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index d91a390..d13549f 100644 --- a/STL/set.md +++ b/STL/set.md @@ -553,4 +553,112 @@ The often used operations in set are : setB :: 1 2 3 ``` -8. **UPPER AND LOWER :** +8. **UPPER AND LOWER BOUND:** +Upper and lower_bound is very important function for certain cases. Using find function you can search for an element if that element is not present in the set then std::find won't be able to find that element. But using Upper and lower bound you can find element which is strictly bigger (smallest number such that this number is bigger than a certain number ~ see the code for better understanding) than a certain number. Lets see the difference between **Upper_bound** and **lower_bound** : + - **UPPER BOUND :** Suppose you are given a set of integers, `S = { 12, 17, 27, 35, 47, 58, 93 }` Here if you want to know the **upper bound of 24**, it will be **27** which means _**`upper_bound(x) will return an iterator to the first element which is greater than x`**_. But in case of upper bound in the example mentioned above the **uppper_bound(35)** will be **47** because 47 is the first integer in the set which is greater than 35 (_not 35 as it is equal_). So, lets see the code for implementation : + + ```cpp + void print_set (set < int > temp){ + for( auto an_element : temp ) + // Taking elements from temp set to an_element one by one + { + // Here an_element is a member of temp set + cout << an_element << " "; + } + cout << endl; + } + + int main(){ + + set < int > MySet; // Declaring MySet + set < int >::iterator it; // Declaring an iterator of integer set + + MySet.insert(12); + MySet.insert(17); + MySet.insert(27); + MySet.insert(35); + MySet.insert(47); + MySet.insert(58); + MySet.insert(93); + // So, Now the set should be : 12 , 17 , 27 , 35, 47, 58, 93 + + cout << "The Elements of the set are : " << endl; + print_set(MySet); // Printing MySet + cout << endl; + + it = MySet.upper_bound(24); + cout << "value of MySet.upper_bound(24) is : " << *it << endl; + // *it will be 27 because 27 is the first number greater than 24 + + it = MySet.upper_bound(35); + cout << "value of MySet.upper_bound(35) is : " << *it << endl; + // *it will be 47 because 47 is the first number greater than 35 + // *it is not 35 because they are equal... + + return 0; + } + ``` + + **output :** + + ```cpp + The Elements of the set are : + 12 17 27 35 47 58 93 + + value of MySet.upper_bound(24) is : 27 + value of MySet.upper_bound(35) is : 47 + ``` + - **LOWER BOUND :** Suppose you are given a set of integers as like before, `S = { 12, 17, 27, 35, 47, 58, 93 }` Here if you want to know the **lower bound of 24**, it will be **27** which means _**`lower_bound(x) will return an iterator to the first element which is greater or equal to x`**_. But in case of lower bound in the example mentioned above the **lower_bound(35)** will be **35** because 35 is equal ot the argumant (_as it is equal_). So, lets see the code for implementation : + + ```cpp + void print_set (set < int > temp){ + for( auto an_element : temp ) + // Taking elements from temp set to an_element one by one + { + // Here an_element is a member of temp set + cout << an_element << " "; + } + cout << endl; + } + + int main(){ + + set < int > MySet; // Declaring MySet + set < int >::iterator it; // Declaring an iterator of integer set + + MySet.insert(12); + MySet.insert(17); + MySet.insert(27); + MySet.insert(35); + MySet.insert(47); + MySet.insert(58); + MySet.insert(93); + // So, Now the set should be : 12 , 17 , 27 , 35, 47, 58, 93 + + cout << "The Elements of the set are : " << endl; + print_set(MySet); // Printing MySet + cout << endl; + + it = MySet.lower_bound(24); + cout << "value of MySet.lower_bound(24) is : " << *it << endl; + // *it will be 27 because 27 is the first number greater or equal to 24 + + it = MySet.lower_bound(35); + cout << "value of MySet.lower_bound(35) is : " << *it << endl; + // *it will be 35 because 35 is equal to the argumant + // *it is will be 47 in case of upper bound + + return 0; + } + ``` + + **output :** + + ```cpp + The Elements of the set are : + 12 17 27 35 47 58 93 + + value of MySet.lower_bound(24) is : 27 + value of MySet.lower_bound(35) is : 35 + ``` + Hope that you are clear with the basic functons of a set. From 507642a8f57cb5eb58e18bf852b2386e65859724 Mon Sep 17 00:00:00 2001 From: Samiul Islam Mugdha <67468157+mugdha-samiul@users.noreply.github.com> Date: Sat, 4 Jul 2020 11:53:33 +0600 Subject: [PATCH 81/97] created queue file --- STL/queue.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 STL/queue.md diff --git a/STL/queue.md b/STL/queue.md new file mode 100644 index 0000000..d985232 --- /dev/null +++ b/STL/queue.md @@ -0,0 +1,3 @@ +# QUEUE + +## INTRODUCTION : From 600851dfc5762fea0a4a7c4d1ebaa1b575fad1cc Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Sat, 4 Jul 2020 12:23:08 +0600 Subject: [PATCH 82/97] Update vector.md --- STL/vector.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/STL/vector.md b/STL/vector.md index 474b69f..d18df1a 100644 --- a/STL/vector.md +++ b/STL/vector.md @@ -314,6 +314,48 @@ We can declare a vector in several ways. Let us see some of them at a glance and | **rbegin()**
`v.rbegin()` | Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first. | | **rend()**
`v.rend()` | Returns a reverse iterator pointing to the first element in the vector (reverse end). | +Let's see their uses through code:- + +```cpp + vector < int > v {1, 2, 3, 4, 5}; + + cout << "Using begin() & end(): "; + for(auto i = v.begin(); i != v.end(); i++) { + cout << *i << " "; + } + cout << endl; + + cout << "Using rbegin() & rend(): "; + for(auto i = v.rbegin(); i != v.rend(); i++) { + cout << *i << " "; + } + cout << endl; +``` + + Output: + Using begin() & end(): 1 2 3 4 5 + Using rbegin() & rend(): 5 4 3 2 1 + +In this code, `i` is used as an iterator. We have used auto to keep it simple. If we do not want to use auto, we have to declare an iterator to vector like this - `vector < int >::iterator i` and a reverse iterator like this `vector < int >::reverse_iterator j;`. The we can use `i` in the first loop and `j` in the second loop.. Then the code will be like :- + +```cpp + vector < int > v {1, 2, 3, 4, 5}; + vector < int >::iterator i; + vector < int >::reverse_iterator j; + + cout << "Using begin() & end(): "; + for(i = v.begin(); i != v.end(); i++) { + cout << *i << " "; + } + cout << endl; + + cout << "Using rbegin() & rend(): "; + for(j = v.rbegin(); j != v.rend(); j++) { + cout << *j << " "; + } + cout << endl; +``` + *** ### Important Capacity Functions From b9fd34e42426cb70a260d60ab57e9476e2513f0f Mon Sep 17 00:00:00 2001 From: Shawon Majid Date: Sat, 4 Jul 2020 20:38:04 +0600 Subject: [PATCH 83/97] Update map.md --- STL/map.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/STL/map.md b/STL/map.md index 45e0896..7b3d7e2 100644 --- a/STL/map.md +++ b/STL/map.md @@ -17,3 +17,46 @@ So, in a map, There are some elements which has a key value and a mapped value. ```cpp map M; ``` +## Basic Operations + +Map is in a way vector of pairs where first element is a key value, which has to be unique(different for multimap we'll come to that later) and second element is mapped value. Now to be clear let's write a code: +```cpp +#include +int main() +{ + map M; + M["Shanto"] = 29; + M["Mehraj"] = 42; + M["Mugdha"] = 1; + cout << M["shanto"] << endl; +} +``` +``` +output: 29 +``` +There are several ways to initialize a map, we could also use: +```cpp +M.insert(make_pair("Shanto", 29)); +``` +Now if we want to print the whole map we can do it by iterators: +```cpp +for(auto it = M.begin(); it != M.end(); it++){ + cout << it->first << ' ' << it -> second << endl; +} +``` +``` +output: +Mehraj 42 +Mugdha 1 +Shanto 29 +``` +Now if we analyze the output we would notice that `our map is not printed in the input order rather it was printed in lexicographical order of key values`. Here, *Mehraj* is lexicographically less then *Mugdha*. That's why *Mehraj* was printed first. +|Key Value| mapped value| +|----------|------------| +|Mehraj|42| +|Mugdha|1| +|Shanto|29| + +Now what will happen if we add same key value? Lets code: + + From 30e6e1aee821a017d9d54a642ee6633277c7fcc1 Mon Sep 17 00:00:00 2001 From: Shawon Majid Date: Sun, 5 Jul 2020 00:55:25 +0600 Subject: [PATCH 84/97] Update map.md --- STL/map.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/STL/map.md b/STL/map.md index 7b3d7e2..10a14f4 100644 --- a/STL/map.md +++ b/STL/map.md @@ -58,5 +58,36 @@ Now if we analyze the output we would notice that `our map is not printed in the |Shanto|29| Now what will happen if we add same key value? Lets code: +```cpp +. +. +M["Shanto"] = 30; +// now print all +``` +``` +output: +Mehraj 42 +Mugdha 1 +Shanto 30 +``` +As you see the mapped value of *Shanto* is updated but it didn't create the same key value again! + +Let's see another example, suppose we're given a sequence of numbers and we need to find the occurence of every distinct number in the sequence. For example, 1, 4, 1, 3. 1, 3, 4 is given. Here occurence of 1 is 3, 3 is 2 and 4 is also 2. We can solve this problem very easily by using map. +```cpp +int main(){ + map M; + int n; + cin >> n; + for(int i = 0; i < n; i++){ + int x; + cin >> x; + M[x]++; + } + for(auto x:M){ + cout << "occurence of "<< x.first << "is: "<< x.second << endl; + } +} +``` +See this code and try to understand what happened here by yourself! From 794aa1387dc596d99e910ebb5acf4f920d7487a7 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 19:47:12 +0600 Subject: [PATCH 85/97] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5c46278..cdb1329 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ - Dynamic Programming - BitSet - Greedy +- Binary Search - Two Pointer - Geometry - Combinatorics From 29831857bad6e66c546d870dc5e2987736886cdf Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 19:48:10 +0600 Subject: [PATCH 86/97] Create BinarySearch.md --- BinarySearch.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 BinarySearch.md diff --git a/BinarySearch.md b/BinarySearch.md new file mode 100644 index 0000000..be39ff2 --- /dev/null +++ b/BinarySearch.md @@ -0,0 +1,3 @@ +# Binary Search + +*** From 2d7c20ec1139caedeb2511f68b81d27667909617 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 19:50:34 +0600 Subject: [PATCH 87/97] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cdb1329..bb7b2b1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ - Dynamic Programming - BitSet - Greedy -- Binary Search +- [Binary Search](https://definecoder.github.io/BinarySearch) - Two Pointer - Geometry - Combinatorics From 5cc81eb2dc4bcb388f0c9654fac80f1525a92074 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 19:54:53 +0600 Subject: [PATCH 88/97] Update BinarySearch.md --- BinarySearch.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/BinarySearch.md b/BinarySearch.md index be39ff2..4bd2369 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -1,3 +1,11 @@ # Binary Search *** + +In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. + +Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. + +Binary search runs in logarithmic time in the worst case, making O(log n) comparisons, where {\displaystyle n}n is the number of elements in the array. Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search. There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search. However, binary search can be used to solve a wider range of problems, such as finding the next-smallest or next-largest element in the array relative to the target even if it is absent from the array. + +There are numerous variations of binary search. In particular, fractional cascading speeds up binary searches for the same value in multiple arrays. Fractional cascading efficiently solves a number of search problems in computational geometry and in numerous other fields. Exponential search extends binary search to unbounded lists. The binary search tree and B-tree data structures are based on binary search. From 5c523ec3ad7cdb7954247355e92fb6da5710b66c Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 20:03:05 +0600 Subject: [PATCH 89/97] Update BinarySearch.md --- BinarySearch.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/BinarySearch.md b/BinarySearch.md index 4bd2369..bda9a39 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -2,10 +2,25 @@ *** -In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. +
+ About Binary Search + +In computer science, **Binary Search**, also known as **half-interval search**, **logarithmic search**, or **binary chop**, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. - -Binary search runs in logarithmic time in the worst case, making O(log n) comparisons, where {\displaystyle n}n is the number of elements in the array. Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search. There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search. However, binary search can be used to solve a wider range of problems, such as finding the next-smallest or next-largest element in the array relative to the target even if it is absent from the array. - + +Binary search runs in logarithmic time in the worst case, making **O(log n)** comparisons, where **n** is the number of elements in the array. Binary search is faster than linear search except for small arrays. However, the array must be sorted first to be able to apply binary search. There are specialized data structures designed for fast searching, such as hash tables, that can be searched more efficiently than binary search. However, binary search can be used to solve a wider range of problems, such as finding the next-smallest or next-largest element in the array relative to the target even if it is absent from the array. + There are numerous variations of binary search. In particular, fractional cascading speeds up binary searches for the same value in multiple arrays. Fractional cascading efficiently solves a number of search problems in computational geometry and in numerous other fields. Exponential search extends binary search to unbounded lists. The binary search tree and B-tree data structures are based on binary search. + +
+ + +### Algorithm + +*** + +Binary search works on sorted arrays. Binary search begins by comparing an element in the middle of the array with the target value. If the target value matches the element, its position in the array is returned. If the target value is less than the element, the search continues in the lower half of the array. If the target value is greater than the element, the search continues in the upper half of the array. By doing this, the algorithm eliminates the half in which the target value cannot lie in each iteration. + +Let `A` be an array, which has `n` elements and we want to search for a number `T`. + From d9b43ddbf74d7ef19faef0f27d0390e152c98bec Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 20:12:29 +0600 Subject: [PATCH 90/97] Update BinarySearch.md --- BinarySearch.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/BinarySearch.md b/BinarySearch.md index bda9a39..ae24011 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -20,7 +20,62 @@ There are numerous variations of binary search. In particular, fractional cascad *** +
+ Explanation + Binary search works on sorted arrays. Binary search begins by comparing an element in the middle of the array with the target value. If the target value matches the element, its position in the array is returned. If the target value is less than the element, the search continues in the lower half of the array. If the target value is greater than the element, the search continues in the upper half of the array. By doing this, the algorithm eliminates the half in which the target value cannot lie in each iteration. + +
+ + + +Let **A** be an array, which has **n** elements and we want to search for a number **T**. The **Pseudocode** of searching for this item is given below: + +
+ Pseudocode + + function binary_search(A, n, T) is + L := 0 + R := n − 1 + while L ≤ R do + m := floor((L + R) / 2) + if A[m] < T then + L := m + 1 + else if A[m] > T then + R := m − 1 + else: + return m + return unsuccessful + +
+ +The **function** for running Binary Search over a sorted array is given below: + +
+ Function + +```cpp + +int BinarySearch ( int DATA[], int LB, int UB, int ITEM ) +{ + int BEG = LB, END = UB, MID; + + while( BEG <= END ) { + MID = ( int ) ( BEG + END ) / 2; + if ( ITEM < DATA[MID] ) { + END = MID - 1; + } + else if ( ITEM > DATA[MID] ) { + BIG = MID + 1; + } + else { + return MID; + } + } + return -1; +} -Let `A` be an array, which has `n` elements and we want to search for a number `T`. +``` + +
From 2d1b239a798ee112246a61ea137e17a8d40829bd Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 20:14:45 +0600 Subject: [PATCH 91/97] Update BinarySearch.md --- BinarySearch.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BinarySearch.md b/BinarySearch.md index ae24011..5d864ab 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -69,10 +69,10 @@ int BinarySearch ( int DATA[], int LB, int UB, int ITEM ) BIG = MID + 1; } else { - return MID; + return MID; // found the item! So returning its index } } - return -1; + return -1; // there is no such item. So returning an impossible index } ``` From 8a168e120e86cbad77f236a732e5fb48bb0146fd Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 20:28:41 +0600 Subject: [PATCH 92/97] Update BinarySearch.md --- BinarySearch.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/BinarySearch.md b/BinarySearch.md index 5d864ab..2b9922e 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -52,7 +52,7 @@ Let **A** be an array, which has **n** elements and we want to search for a numb The **function** for running Binary Search over a sorted array is given below:
- Function + Function of Binary Search ```cpp @@ -78,4 +78,71 @@ int BinarySearch ( int DATA[], int LB, int UB, int ITEM ) ```
+ + +
+ More about the function of Binary Search + +In this function - + +```cpp + +int DATA[] = the dataset given to us. +int LB = the lower bound of the range we want to search for. +int UB = the upper bound of the range we want to search for. +int ITEM = the item which we are searching. + +``` + +Suppose, we have a sorted array **DATA[8] = { 10, 20, 20, 20, 30, 30, 40, 50 }**. We would like to search if **40** is present in this array or not. And we want to search in the whole array. In this case, + +```cpp + +int LB = 0; +int UB = 7; +int ITEM = 40 + +``` + +Then we call the function to get the location of the item. + +```cpp + +int index = BinarySearch ( DATA[], LB, UB, ITEM ); +if( index == -1 ) std::cout << "Item Not Found" << endl; +else std::cout << "Item Found at Index " << index << endl; + +``` + +``` + +Output: +Item Found at Index 6 + +``` + +If we set `ITEM = 25`, the output will be - `Item Not Found`. + +
+ + + + + + + + + + + + + + + + + + + + + From cf5c718c9086d1581dba153015077eed8e2865a5 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 20:31:54 +0600 Subject: [PATCH 93/97] Update BinarySearch.md --- BinarySearch.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/BinarySearch.md b/BinarySearch.md index 2b9922e..6ddf48a 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -62,11 +62,11 @@ int BinarySearch ( int DATA[], int LB, int UB, int ITEM ) while( BEG <= END ) { MID = ( int ) ( BEG + END ) / 2; - if ( ITEM < DATA[MID] ) { + if ( ITEM < DATA[MID] ) { // ITEM < DATA[MID]. Update END value END = MID - 1; } - else if ( ITEM > DATA[MID] ) { - BIG = MID + 1; + else if ( ITEM > DATA[MID] ) { // ITEM > DATA[MID]. Update BEG value + BEG = MID + 1; } else { return MID; // found the item! So returning its index From b1569ccbb7e876938982ee32ff18d7760b457320 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 20:48:23 +0600 Subject: [PATCH 94/97] Update BinarySearch.md --- BinarySearch.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/BinarySearch.md b/BinarySearch.md index 6ddf48a..45e5241 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -108,7 +108,7 @@ Then we call the function to get the location of the item. ```cpp -int index = BinarySearch ( DATA[], LB, UB, ITEM ); +int index = BinarySearch ( DATA[], 0, 8, 40 ); if( index == -1 ) std::cout << "Item Not Found" << endl; else std::cout << "Item Found at Index " << index << endl; @@ -122,6 +122,23 @@ Item Found at Index 6 ``` If we set `ITEM = 25`, the output will be - `Item Not Found`. + +If we do not want to run Binary Search over the whole array but on a section of the array, we just have to set the boundary. Suppose we want to run over from index 2 to index 5 of the array, then our code will look like - + +```cpp + +int index = BinarySearch ( DATA[], 2, 5, 40 ); + +``` + +``` + +Output: +Item Not Found. + +``` + +This is the most simple version of Binary Search. We can also use Binary Search to find some other things too, such as - we can find **Lower Bound** and **Upper Bound** of an item, **smallest element** and **largest element** from a rotated (after sorted) array, we can also solve some other problems like **Max in a hill** or **Min in a canyon**. We will be exploring them in the further part. From 4cdfb58cc1d2913772a721e08957199522eff278 Mon Sep 17 00:00:00 2001 From: Ariful Islam Shanto Date: Tue, 3 Nov 2020 20:49:53 +0600 Subject: [PATCH 95/97] Update BinarySearch.md --- BinarySearch.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/BinarySearch.md b/BinarySearch.md index 45e5241..a153213 100644 --- a/BinarySearch.md +++ b/BinarySearch.md @@ -115,10 +115,8 @@ else std::cout << "Item Found at Index " << index << endl; ``` ``` - Output: Item Found at Index 6 - ``` If we set `ITEM = 25`, the output will be - `Item Not Found`. @@ -132,10 +130,8 @@ int index = BinarySearch ( DATA[], 2, 5, 40 ); ``` ``` - Output: Item Not Found. - ``` This is the most simple version of Binary Search. We can also use Binary Search to find some other things too, such as - we can find **Lower Bound** and **Upper Bound** of an item, **smallest element** and **largest element** from a rotated (after sorted) array, we can also solve some other problems like **Max in a hill** or **Min in a canyon**. We will be exploring them in the further part. From b8734c5eb1dbae14c9203c17225a415888efac63 Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Wed, 10 Mar 2021 11:38:14 +0600 Subject: [PATCH 96/97] Added user input --- STL/set.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/STL/set.md b/STL/set.md index d13549f..9d00984 100644 --- a/STL/set.md +++ b/STL/set.md @@ -54,7 +54,28 @@ The often used operations in set are : return 0; } ``` - Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user. + Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user. You can do that using a varible. See the code below or better understanding: + ```cpp + #include + #include + + using namespace std; + + int main(){ + + set < int > MySet; // Declaring MySet + int x; // variable to take input + int n; // how many number you want to insert + + for(int i = 0 ; i < n ; i++) + { + cin >> x; + Myset.insert(x); // inserting x into the MySet + } + + return 0; + } + ``` 2. **ITERATOR :** Iterators are used to point out an element in the set as well as any other container in STL. [You can check this link to know more about iterators](https://definecoder.github.io/STL/iterator). Iterators are very important to access any element in the set. Lets see the list of the iterators for set and the code: | Function | Work of the function | @@ -139,7 +160,7 @@ The often used operations in set are : ``` 3. **PRINTING A SET :** Printing of a set can be done in two ways. First one is using iterator and the second one is using C++11 short cut feature. We will include both here. Lets see: - - _**Using Iterator**_ : As you know iterator is a poiunter for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. + - _**Using Iterator**_ : As you know iterator is a pointer for the STL datatype or containers, It is very useful to access the elements in a set. First of all you need to declare an iterator of the type of the set that you want to work with. Then you have to iterate through the set using that iterator that you declared. Lest see the code for better understanding. ```cpp #include From 48bf0c79bfac344ee81ce573f44ca1e737ea13fa Mon Sep 17 00:00:00 2001 From: Md Mehrajul Islam <47929493+codermehraj@users.noreply.github.com> Date: Wed, 10 Mar 2021 11:40:40 +0600 Subject: [PATCH 97/97] Update set.md --- STL/set.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STL/set.md b/STL/set.md index 9d00984..de84c16 100644 --- a/STL/set.md +++ b/STL/set.md @@ -55,7 +55,7 @@ The often used operations in set are : } ``` Here you can see when we tried to insert 45 for the second time it was not inserted into the set. You can also insert variables which are taken from the user. You can do that using a varible. See the code below or better understanding: - ```cpp + ```cpp #include #include