├── README.md
└── assets
└── images
├── CPPSTL.png
├── save.png
└── test
/README.md:
--------------------------------------------------------------------------------
1 | # C\+\+ STL-Library 📚📚
2 |
3 | In C++, the Standard Template Library (STL) provides a set of programming tools to implement algorithms and data structures like vectors, lists, queues, etc. This repository contains entire C++ STL Library which anyone require while doing Competitive Programming, Contest solving or in Online Assessment.
4 |
5 | ---
6 |
7 | ###
8 |
9 | #### Join us for all the latest offcampus job updates, webinar, hackathons, resume review and a lot more :heart::heart:
10 |
11 |
28 |
29 | ###
30 |
31 | ---
32 |
33 | ## 🚀 Table of Contents
34 |
35 | - [Vector In C++](#vector-in-c)
36 | - [Stack In C++](#stack-in-c)
37 | - [Queue In C++](#queue-in-c)
38 | - [Deque In C++](#deque-in-c)
39 | - [Priotity Queue In C++](#priority-queue-in-c)
40 | - [Set In C++](#set-in-c)
41 | - [Unordered Set In C++](#unordered-set-in-c)
42 | - [Multiset In C++](#multiset-in-c)
43 | - [Unordered Multiset In C++](#unordered-multiset-in-c)
44 | - [Map In C++](#maps-in-c)
45 | - [Unordered Map In C++](#unordered-maps-in-c)
46 | - [MultiMap In C++](#multimaps-in-c)
47 | - [Unordered MultiMap In C++](#unordered-multimaps-in-c)
48 |
49 | ---
50 |
51 |
52 |
53 | ---
54 |
55 | # VECTOR IN C++
56 |
57 | A **vector** is a dynamic array whose size can be changed automatically when an element is inserted or deleted. Vector elements are stored in contiguous memory locations so that they can be accessed in constant time.
58 |
59 | It is defined inside the `` header file (also available in ``).
60 |
61 | The complexity (efficiency) of common operations on vectors is as follows:
62 |
63 | - Random access - constant 𝓞(1).
64 | - Insertion or removal of elements at the end - amortized constant 𝓞(1).
65 | - Insertion or removal of elements in front or middle of the vector 𝓞(n).
66 |
67 | ## Ways to declare a vector
68 |
69 | ### Syntax
70 |
71 | - `vector vec;` *Empty*
72 | - `vector vec( size );` *Vector with size 'size'*
73 | - `vector vec( size, value );` *Vector with size 'size' and all elements with value 'value'*
74 | - `vector vec = { value1, value2, value3,...,valueN};` *Vector with N values*
75 |
76 | ### Examples
77 |
78 | ```cpp
79 | vector vec; // Declares an empty vector with name as vec ---> []
80 | vector vec(5); // Declares vector of ints with size 5 and the default values assigned are 0s ---> [0,0,0,0,0]
81 | vector vec(5, 10); // Declares vector of ints with size 5 and values assigned are 10s ---> [10,10,10,10,10]
82 | vector vec = {1, 2, 3, 4}; // Declares vector with size 4 and values as ---> [1, 2, 3, 4]
83 | vector vect(5) ; // Declares vector with 5 empty strings ---> ["", "", "", "", ""]
84 | vector vec(5, "Leetcode"); // Declares vector of strings with size 5 and values initialised are "Leetcode" ---> ["Leetcode", "Leetcode", "Leetcode", "Leetcode", "Leetcode"]
85 | vector> vec(5); // Declares vector of vectors ---> [[], [], [], [], []]
86 | vector> vec(5, vector(2)); // Declares vector of vectors(here inside vector size is 2 and default values assigned are 0's) ---> [[0,0], [0,0], [0,0], [0,0], [0,0]]
87 | vector> vec(5, vector(2, 10)); // Declares vector of vectors(here inside vector size is 2 and default values assigned are 10's) ---> [[10,10], [10,10],[10,10],[10,10],[10,10]]
88 |
89 | ```
90 |
91 |
92 | ## Ways to Insert Elements into Vector
93 |
94 | ### Syntax
95 |
96 | - `vec.push_back( ele );` Adds an element 'ele' at the end, and size of the vector increases by 1 (TC - 𝓞(1))
97 | - `vec.emplace_back( ele );` Adds an element 'ele' at the end, and size of the vector increases by 1 (TC - 𝓞(1))
98 | - `vec.insert( vec.begin() + i, ele );` Inserting element 'ele' at index i | Use i = 0 for inserting at the front (TC - 𝓞(n))
99 | - `vec.insert( vec.end(), vec2.begin(), vec2.end() );` Inserting the elements of vector 'v2' to the end of vector 'v' (TC - 𝓞(n))
100 |
101 | ## Ways to Delete Elements from Vector
102 |
103 | ### Syntax
104 |
105 | - `v.pop_back();` Remove element at the end (TC - 𝓞(1))
106 | - `v.erase( v.begin() + i );` Remove element at index i or i = 0 to remove the first element (TC - 𝓞(n))
107 | - `v.erase( v.begin() + i, v.begin() + j );` Remove elements from index i to j-1 (TC - 𝓞(n))
108 | - `v.erase( remove( v.begin(), v.end(), value ), v.end() );` Removes all elements with value 'value' from the vector (TC - 𝓞(n))
109 |
110 | ## Basic Inbuilt Functions
111 |
112 | ### Syntax
113 |
114 | - `v.size();` Returns the size of vector (TC - 𝓞(1))
115 | - `v.resize( size, val );` If the 'size' is greater than the previous size, then it adds elements with value 'val' at end and if 'size' is less than previous, then it removes from end
116 | - `v.empty();` Returns true if the vector is empty (TC - 𝓞(1))
117 | - `v.front();` or `v[0];` Accessing the first element (TC - 𝓞(1))
118 | - `v.back();` or `v[v.size()-1];` Accessing the last element (TC - 𝓞(1))
119 | - `v[i];` Accessing element at i'th index (0-based) (TC - 𝓞(1))
120 | - `v.at(i);` Accessing element at i'th index (0-based) (TC - 𝓞(1))
121 | - `v.clear();` Clears vector elements by making vector as empty (TC - 𝓞(n))
122 |
123 | ## Some Algorithmic Based Functions
124 |
125 | - `sort(v.begin(), v.end());` Sorts vector elements in ascending order by default (TC - 𝓞(nlogn))
126 | - `sort(v.begin(), v.end(), greater);` Sorts vector elements in descending order (TC - 𝓞(nlogn))
127 | - `reverse(v.begin(), v.end());` Reverses vector elements (TC - 𝓞(n))
128 | - `count(v.begin(), v.end(), x);` Returns the count of x in vector (TC - 𝓞(n))
129 | - `min_element(v.begin(), v.end()) - v.begin();` Returns the minimum element's index (0-based) from the vector (TC - 𝓞(n))
130 | - `max_element(v.begin(), v.end()) - v.begin();` Returns the maximum element's index (0-based) from the vector (TC - 𝓞(n))
131 | - `*min_element(v.begin(), v.end());` Returns the minimum element from the vector (TC - 𝓞(n))
132 | - `*max_element(v.begin(), v.end());` Returns the maximum element from the vector (TC - 𝓞(n))
133 |
134 | *Note: The difference between `v[i]` and `v.at(i)` is that 'at' will raise an exception if you try to access an element outside the vector while the `[]` operator won't.*
135 |
136 |
137 |
138 | *Lets undertand by sample program*
139 |
140 | ```cpp
141 | #include
142 | using namespace std;
143 |
144 | int main() {
145 | vector v = {1, 3, 2}; // Create a vector containing integers ---> [1, 3, 2]
146 |
147 | // Insertion
148 | v.push_back(5); // Adding 5 at the end ---> [1, 3, 2, 5]
149 | v.emplace_back(4); // Adding 4 at the end ---> [1, 3, 2, 5, 4]
150 | v.insert(v.begin(), 10); // Inserts 10 at the beginning ---> [10, 1, 3, 2, 5, 4]
151 | v.insert(v.begin() + 3, 15); // Inserts 15 at 3rd index ---> [10, 1, 3, 15, 2, 5, 4]
152 |
153 | // Deletion
154 | v.pop_back(); // Removes an element at the end ---> [10, 1, 3, 15, 2, 5]
155 | v.erase(v.begin()); // Removes an element at the front ---> [1, 3, 15, 2, 5]
156 | v.erase(v.begin() + 3); // Removes an element at 3rd index ---> [1, 3, 15, 5]
157 |
158 | // Accessing and Updating
159 | int a = v[3]; // Here the variable a stores the value at 3rd index i.e., 5
160 | v[1] = 100; // Here the value at index 1 is updated with 100 ---> [1, 100, 15, 5]
161 | int last_element = v.back(); // i.e., 5
162 | int first_element = v.front(); // i.e., 1
163 |
164 | sort(v.begin(), v.end()); // Sorts vector elements ---> [1, 5, 15, 100]
165 | reverse(v.begin(), v.end()); // Reverses vector elements ---> [100, 15, 5, 1]
166 |
167 | // Traversing the vector using a for loop
168 | for (int i = 0; i < v.size(); i++) {
169 | cout << v[i] << " "; // After the for loop, Output is: 100 15 5 1
170 | }
171 | int length = v.size(); // Here the variable length stores the vector size i.e., 4
172 | v.clear(); // Vector is cleared ---> []
173 | bool isVectorEmpty = v.empty(); // Here the variable isVectorEmpty stores 'true' as the vector is empty
174 |
175 | return 0;
176 | }
177 |
178 |
179 | ```
180 |
181 | ---
182 |
183 | # STACK IN C++
184 |
185 | - A stack is a linear data structure that follows the principle of `Last In First Out (LIFO)`. This means the last element inserted inside the stack is removed first.
186 | - It is defined inside `` header file (also available in ``).
187 |
188 | ## Declaring a Stack
189 |
190 | ### Syntax
191 | ```cpp
192 | stack< data_type > stack_name;
193 | ```
194 |
195 | ### Examples
196 |
197 | - `stack stk;` stack of int's
198 | - `stack stk;` stack of strings
199 | - `stack> stk;` stack of pairs
200 | - `stack> stck;` stack of vectors
201 |
202 | *Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.*
203 |
204 | ## Important Functions on Stack
205 |
206 | - `stk.push(ele);` pushes an element 'ele' into the stack TC - 𝓞(1)
207 | - `stk.pop();` removes the top element TC - 𝓞(1)
208 | - `stk.top();` returns the topmost element TC - 𝓞(1)
209 | - `stk.size();` returns the size of stack TC - 𝓞(1)
210 | - `stk.empty();` returns true if the stack is empty else false TC - 𝓞(1)
211 |
212 | *Note: The time complexity of all above inbuilt functions is constant - 𝓞(1)*
213 |
214 | ## Accessing Stack Elements
215 |
216 | Since it won't provide indexing, *we cannot directly access any element except the top element.* The below is the way to traverse the stack.
217 | ```cpp
218 | while(!stk.empty()){
219 | cout << stk.top() << " ";
220 | stk.pop();
221 | }
222 | ```
223 |
224 | *Lets understand by sample program*
225 |
226 | ```cpp
227 | #include
228 | using namespace std;
229 |
230 | int main() {
231 | stack stk;
232 |
233 | //pushing data into the stack
234 | stk.push("Hi");
235 | stk.push("Hello");
236 | stk.push("How are you?");
237 |
238 | //poping data from top of the stack
239 | stk.pop(); // the top element i.e "How are you?" is removed;
240 |
241 | cout << stk.top() << " "; // prints top element "Hello"
242 | cout << stk.size() << " "; // prints 2
243 | cout << stk.empty(); //prints false | 0
244 |
245 | return 0;
246 | }
247 | ```
248 |
249 | ---
250 |
251 | # QUEUE IN C++
252 |
253 | - A queue is a linear data structure that follows the `FIFO (First In First Out)` principle, i.e., elements that are added first will be removed first.
254 | - It is defined inside `` header file (also available in ``).
255 |
256 |
257 | ## Declaring a Queue
258 |
259 | ### Syntax
260 | ```cpp
261 | queue< data_type > queue_name;
262 | ```
263 |
264 | ### Examples
265 |
266 | - `queue q;` queue of int's
267 | - `queue q;` queue of strings
268 | - `queue> q;` queue of pairs
269 | - `queue> q;` queue of vectors
270 |
271 | *Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.*
272 |
273 | ## Important Functions on Queue
274 |
275 | - `q.push(ele);` pushes an element 'ele' into the queue from the end TC - 𝓞(1)
276 | - `q.pop();` removes an element from the front of the queue TC - 𝓞(1)
277 | - `q.front();` returns the first element TC - 𝓞(1)
278 | - `q.back();` returns the last element TC - 𝓞(1)
279 | - `q.size();` returns the size of the queue TC - 𝓞(1)
280 | - `q.empty();` returns true if the queue is empty else false TC - 𝓞(1)
281 |
282 | *Note: The time complexity of all above inbuilt functions is constant - 𝓞(1)*
283 |
284 | ## Accessing Queue Elements
285 |
286 | Since it won't provide indexing, we cannot directly access any element except the first and last element. The below is the way to traverse the queue.
287 |
288 | ```cpp
289 | while(!q.empty()){
290 | cout << q.front() << " ";
291 | q.pop();
292 | }
293 | ```
294 |
295 | *Let's understand with a sample program*
296 |
297 | ```cpp
298 | #include
299 | using namespace std;
300 |
301 | int main() {
302 |
303 | queue q;
304 |
305 | // Pushing data into the queue
306 | q.push("Hi");
307 | q.push("Hello");
308 | q.push("How are you?");
309 |
310 | // Popping data from the top of the queue
311 | q.pop(); // The front element, i.e., "Hi", is removed;
312 |
313 | cout << q.front() << " "; // Prints the front element, "Hello"
314 | cout << q.back() << " "; // Prints the back element, "How are you?"
315 | cout << q.size() << " "; // Prints 2
316 | cout << q.empty(); // Prints false | 0
317 |
318 | return 0;
319 | }
320 | ```
321 |
322 | ---
323 |
324 | # DEQUE IN C++
325 |
326 | Deque is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end.
327 |
328 | The complexity (efficiency) of common operations on deques is as follows:
329 | - Random access - constant 𝓞(1)
330 | - Insertion or removal of elements at the end or beginning - constant 𝓞(1)
331 | - Insertion or removal of elements - linear 𝓞(n)
332 |
333 | Formally, a deque supports all the functions that a vector supports. The difference between a deque and a vector is that deques do not guarantee that their elements are contiguous in memory, so accessing may not be as efficient.
334 |
335 | ## Ways to Declare a Deque
336 |
337 | ### Syntax
338 | ```cpp
339 | deque< data_type > deque_name;
340 | ```
341 |
342 | ### Examples
343 | - `deque dq;` Deque of int (Empty)
344 | - `deque dq( size );` Deque with size 'size'
345 | - `deque dq( size, value );` Deque with size 'size' and all elements with value 'value'
346 | - `deque dq = { value1, value2, value3,...,valueN};` Deque with N values
347 |
348 |
349 | *Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.*
350 |
351 | ## Important Functions
352 |
353 | - `dq.push_back( ele );` Adds an element 'ele' at the end (TC - 𝓞(1))
354 | - `dq.pop_back();` Removes an element at the end (TC - 𝓞(1))
355 | - `dq.push_front( ele );` Adds an element 'ele' at the front (TC - 𝓞(1))
356 | - `dq.pop_front();` Removes an element at the front (TC - 𝓞(1))
357 | - `dq.size();` Returns the size of the deque (TC - 𝓞(1))
358 | - `dq.empty();` Returns true if the deque is empty, else false (TC - 𝓞(1))
359 | - `dq.front();` or `dq[0];` Accesses the first element (TC - 𝓞(1))
360 | - `dq.back();` or `dq[dq.size()-1];` Accesses the last element (TC - 𝓞(1))
361 | - `dq[i];` Accesses the element at the i'th index (0-based) (TC - 𝓞(1))
362 | - `dq.at(i);` Accesses the element at the i'th index (0-based) (TC - 𝓞(1))
363 |
364 |
365 | ---
366 |
367 | # PRIORITY QUEUE IN C++
368 |
369 | - A priority queue is a special type of queue in which each element is associated with a priority value, and elements are served based on their priority.
370 | - It is defined inside the `` header file (also available in ``).
371 | - By default, C++ creates a max priority queue. We can change it to a min priority queue by passing additional parameters during declaration.
372 |
373 |
374 | ## Syntax
375 |
376 | ### Max - Priority Queue
377 | ```cpp
378 | priority_queue< data_type > priority_queue_name;
379 | ```
380 |
381 | ### Max - Priority Queue
382 | ```cpp
383 | priority_queue< data_type, vector< data_type >, greater< data_type > > priority_queue_name;
384 | ```
385 |
386 | ## Examples
387 |
388 | ### Max Priority Queue
389 | ```cpp
390 | priority_queue q; // max priority_queue of int's
391 | priority_queue q; // max priority_queue of strings
392 | priority_queue> q; // max priority_queue of pairs
393 | priority_queue> q; // max priority_queue of vectors
394 | ```
395 |
396 | ### Min Priority Queue
397 | ```cpp
398 | priority_queue, greater> q; // min priority_queue of int's
399 | priority_queue, greater> q; // min priority_queue of strings
400 | priority_queue, vector>, greater>> q; // min priority_queue of pairs
401 | priority_queue, vector>, greater>> q; // min priority_queue of vectors
402 | ```
403 |
404 | *Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.*
405 |
406 | ## Important functions on priority_queue
407 |
408 | - `pq.push(ele);` Inserts the element 'ele' into the priority queue. [Time Complexity: 𝓞(logn)]
409 | - `pq.pop();` Removes the element with the highest priority. [Time Complexity: 𝓞(logn)]
410 | - `pq.top();` Returns the element with the highest priority. [Time Complexity: 𝓞(1)]
411 | - `pq.size();` Returns the size of the priority_queue. [Time Complexity: 𝓞(1)]
412 | - `pq.empty();` Returns true if the priority_queue is empty, else false. [Time Complexity: 𝓞(1)]
413 |
414 | ## Accessing priority_queue elements
415 |
416 | Since it won't provide indexing, we cannot directly access any element except the top element. The below is the way to traverse the priority_queue.
417 |
418 | ```cpp
419 | while (!pq.empty()) {
420 | cout << pq.top() << " ";
421 | pq.pop();
422 | }
423 | ```
424 |
425 | *Lets understand by sample program*
426 |
427 | ```cpp
428 | #include
429 | using namespace std;
430 |
431 | int main() {
432 | priority_queue pq;
433 |
434 | // Pushing data into the priority_queue
435 | pq.push(6);
436 | pq.push(10);
437 | pq.push(0);
438 | pq.push(-40);
439 | pq.push(8);
440 | pq.push(-20);
441 | pq.push(3);
442 |
443 | cout << pq.top() << " "; // Prints the top element, i.e., 10
444 |
445 | // Popping data from the top of the queue
446 | pq.pop(); // The top element, i.e., 10, is removed;
447 |
448 | cout << pq.top() << " "; // Prints the current top element, i.e., 8
449 | cout << pq.size() << " "; // Prints 6
450 | cout << pq.empty(); // Prints false | 0
451 |
452 | return 0;
453 | }
454 | ```
455 |
456 | ---
457 |
458 | # SET IN C++
459 |
460 | - A set data structure `won't allow duplicate keys`.
461 | - Keys are sorted in ascending order by default. We can also change the order by passing extra arguments during its declaration.
462 | - Implemented using `Binary search tree` (Red-Black trees).
463 |
464 | ## Declaring set
465 |
466 | ### Syntax
467 | ```cpp
468 | set< data_type > st; stores keys in ascending order
469 | set< data_type, greater< data_type > > st; stores keys in descending order
470 | ```
471 |
472 | ### Examples
473 |
474 | - `set st;` set of int's
475 | - `set st;` set of strings
476 | - `set> st;` set of pairs
477 | - `set> st;` set of vectors
478 |
479 |
480 | *Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.*
481 |
482 | ## Important functions on set
483 |
484 | - `st.insert( key );` Inserts the element into the set (if not present). TC - 𝓞(logn)
485 | - `st.erase( key );` Removes the specified key if present. TC - 𝓞(logn)
486 | - `st.find( key );` Returns an iterator pointing to the key; if the key is not present, returns st.end(). TC - 𝓞(logn)
487 | - `st.size();` Returns the size of the set. TC - 𝓞(1)
488 | - `st.empty();` Returns true if the set is empty, else false. TC - 𝓞(1)
489 | - `st.clear();` Removes all set elements. TC - 𝓞(n)
490 |
491 | ## Accessing set elements
492 |
493 | Since it won't provide indexing, we cannot directly access any element. The below is the way to traverse the set.
494 |
495 | ```cpp
496 | for (auto x : st) {
497 | cout << x << " "; // Prints each key in ascending order
498 | }
499 | ```
500 |
501 | *Lets understand by sample program*
502 |
503 | ```cpp
504 | #include
505 | using namespace std;
506 |
507 | int main() {
508 | set st;
509 |
510 | // Inserting data into the set
511 | st.insert(1); // Inserts 1 into the set
512 | st.insert(1); // Since 1 is already present, it won't insert 1 again into the set
513 | st.insert(5); // Inserts 5 into the set
514 | st.insert(10); // Inserts 10 into the set
515 | st.insert(15); // Inserts 15 into the set
516 | st.insert(5); // Since 5 is already present, it won't insert 5 again into the set
517 |
518 | if (st.find(100) != st.end()) {
519 | cout << "Key is present ";
520 | } else {
521 | cout << "Key is not present "; // Prints "Key is not present"
522 | }
523 |
524 | if (st.find(1) != st.end()) {
525 | cout << "Key is present "; // Prints "Key is present" since 1 is present in the set
526 | } else {
527 | cout << "Key is not present ";
528 | }
529 |
530 | st.erase(1); // Removes 1 from the set
531 |
532 | cout << st.size() << " "; // Prints 3
533 | cout << st.empty(); // Prints false | 0
534 | st.clear(); // Removes all elements.
535 |
536 | return 0;
537 | }
538 | ```
539 |
540 | ---
541 |
542 | # Unordered Set in C++
543 |
544 | - An `unordered_set` is similar to the set data structure and won't allow duplicate keys.
545 | - The keys are `not sorted` in any order (No order), and hence the ordering of elements cannot be expected.
546 | - It is implemented using `Hash Tables`.
547 | - Faster in insertion / removal / find than set in average case i.e TC - O(1). ( worst case TC - O(n))
548 |
549 | ## Declaring unordered_set
550 |
551 | ### Syntax
552 | ```cpp
553 | unordered_set< data_type > st;
554 | ```
555 |
556 | ### Examples
557 |
558 | - `unordered_set st;` unordered_set of int's
559 | - `unordered_set st;` unordered_set of strings
560 | - `unordered_set> st;` unordered_set of pairs
561 | - `unordered_set> st;` unordered_set of vectors
562 |
563 |
564 | *Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.*
565 |
566 | ## Important functions on unordered_set
567 |
568 | - `st.insert(key);`
569 | - Inserts the element 'key' into the set (if not present).
570 | - **Average Time Complexity:** 𝓞(1)
571 | - **Worst Time Complexity:** O(n)
572 |
573 | - `st.erase(key);`
574 | - Removes the specified key if present.
575 | - **Average Time Complexity:** 𝓞(1)
576 | - **Worst Time Complexity:** O(n)
577 |
578 | - `st.find(key);`
579 | - Returns an iterator pointing to the key. If the key is not present, returns `st.end()`.
580 | - **Average Time Complexity:** 𝓞(1)
581 | - **Worst Time Complexity:** O(n)
582 |
583 | - `st.size();`
584 | - Returns the size of the set.
585 | - **Time Complexity:** 𝓞(1)
586 |
587 | - `st.empty();`
588 | - Returns true if the set is empty, else false.
589 | - **Time Complexity:** 𝓞(1)
590 |
591 | - `st.clear();`
592 | - Removes all set elements.
593 | - **Time Complexity:** O(n)
594 |
595 | ## Accessing unordered_set elements
596 |
597 | Since it won't provide indexing, we cannot directly access any element. The below is the way to traverse the unordered_set.
598 |
599 | ```cpp
600 | for (auto x : st) {
601 | cout << x << " "; // prints each key in ascending order
602 | }
603 | ```
604 |
605 | *Lets understand by sample program*
606 |
607 | ```cpp
608 | #include
609 | using namespace std;
610 |
611 | int main() {
612 |
613 | unordered_set st;
614 |
615 | // Inserting data into the set
616 | st.insert(1); // Inserts 1 into set
617 | st.insert(1); // Since 1 is already present, it won't insert 1 again into set
618 | st.insert(5); // Inserts 5 into set
619 | st.insert(10); // Inserts 10 into set
620 | st.insert(15); // Inserts 15 into set
621 | st.insert(5); // Since 5 is already present, it won't insert 5 again into set
622 |
623 | if (st.find(100) != st.end()) {
624 | cout << " Key is present ";
625 | } else {
626 | cout << " Key is not present "; // Prints "Key is not present"
627 | }
628 |
629 | if (st.find(1) != st.end()) {
630 | cout << " Key is present "; // Prints "Key is present" since 1 is present in set
631 | } else {
632 | cout << " Key is not present ";
633 | }
634 |
635 | st.erase(1); // 1 removed from set
636 |
637 | cout << st.size() << " "; // Prints 3
638 | cout << st.empty(); // Prints false | 0
639 | st.clear(); // Removes all elements.
640 |
641 | return 0;
642 | }
643 | ```
644 |
645 | ---
646 |
647 | # MULTISET IN C++
648 |
649 | - A multiset data structure `allows multiple keys with the same values`.
650 | - Keys are sorted in ascending order by default. We can also change the order by passing extra arguments during its declaration.
651 | - Implemented using `Binary search tree` (Red-black trees).
652 |
653 | ## Declaring multiset
654 |
655 | ### Syntax
656 |
657 | ```cpp
658 | multiset< data_type > st; Stores keys in ascending order
659 | multiset< data_type, greater< data_type > > st; Stores keys in descending order
660 | ```
661 |
662 | ### Examples
663 |
664 | - `multiset st;` Multiset of int's
665 | - `multiset st;` Multiset of strings
666 | - `multiset> st;` Multiset of pairs
667 | - `multiset> st;` Multiset of vectors
668 |
669 |
670 | Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.
671 |
672 | ## Important Functions on Multiset
673 |
674 | - `st.insert( key );` Inserts the element into the multiset. TC - 𝓞(logn)
675 | - `st.erase( key );` Removes all occurrences of the specified key if present. TC - 𝓞(logn)
676 | - `st.erase( st.find( key ) );` Removes only one occurrence of the specified key if present. TC - 𝓞(logn)
677 | - `st.find( key );` Returns an iterator pointing to the key; if the key is not present, returns `st.end()`. TC - 𝓞(logn)
678 | - `st.count( key );` Returns the frequency of the specified key. TC - 𝓞(logn)
679 | - `st.size();` Returns the size of the multiset. TC - 𝓞(1)
680 | - `st.empty();` Returns true if the multiset is empty; else, false. TC - 𝓞(1)
681 | - `st.clear();` Removes all multiset elements. TC - O(n)
682 |
683 | ## Accessing Multiset Elements
684 | Since it won't provide indexing, we cannot directly access any element. The below is the way to traverse the multiset.
685 |
686 | ```cpp
687 | for( auto x : st ){
688 | cout << x << " "; // Prints each key in ascending order
689 | }
690 | ```
691 |
692 | *Lets understand by sample program*
693 |
694 | ```cpp
695 | #include
696 | using namespace std;
697 |
698 | int main() {
699 |
700 | multiset st;
701 |
702 | // Inserting data into the multiset
703 | st.insert(1); // Inserts 1 into the multiset
704 | st.insert(1); // Inserts 1 into the multiset
705 | st.insert(1); // Inserts 1 into the multiset
706 | st.insert(5); // Inserts 5 into the multiset
707 | st.insert(10); // Inserts 10 into the multiset
708 | st.insert(15); // Inserts 15 into the multiset
709 | st.insert(5); // Inserts 5 into the multiset
710 |
711 | if( st.find(100) != st.end()){
712 | cout << " Key is present " << " ";
713 | }
714 | else {
715 | cout << " Key is not present "; // Prints key is not present
716 | }
717 |
718 | if( st.find(1) != st.end()){
719 | cout << " Key is present " << ; // Prints key is present since 1 is present in the multiset
720 | }
721 | else {
722 | cout << " Key is not present ";
723 | }
724 |
725 | cout << st.count( 1 ) ; // Prints 3
726 |
727 | st.erase( st.find(1) ); // Removes one occurrence of 1
728 |
729 | cout << st.count(1) ; // Prints 2
730 |
731 | st.erase( 1 ); // Removes all occurrences of 1
732 |
733 | cout << st.count(1) ; // Prints 0
734 |
735 | cout << st.size() << " "; // Prints 4
736 | cout << st.empty(); // Prints false | 0
737 | st.clear(); // Removes all elements.
738 |
739 | return 0;
740 | }
741 | ```
742 |
743 | ---
744 |
745 | # Unordered Multiset in C++
746 |
747 | - An `unordered_multiset` data structure allows multiple keys with the same values.
748 | - Keys are `not sorted`, i.e., there is no particular order.
749 | - Implemented using `Hash Tables`.
750 |
751 | ## Declaring Multiset
752 |
753 | ### Syntax
754 |
755 | ```cpp
756 | unordered_multiset< data_type > st;
757 | ```
758 |
759 | ### Examples
760 |
761 | - `unordered_multiset st;` unordered_multiset of int's
762 | - `unordered_multiset st;` unordered_multiset of strings
763 | - `unordered_multiset> st;` unordered_multiset of pairs
764 | - `unordered_multiset> st;` unordered_multiset of vectors
765 |
766 |
767 | Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.
768 |
769 | ## Important functions on unordered_multiset
770 |
771 | - `st.insert( key );`
772 | - Inserts the element into the multiset.
773 | - Average Time Complexity: 𝓞(1)
774 | - Worst Time Complexity: 𝓞(n)
775 |
776 | - `st.erase( key );`
777 | - Removes all occurrences of the specified key if present.
778 | - Average Time Complexity: 𝓞(1)
779 | - Worst Time Complexity: 𝓞(n)
780 |
781 | - `st.erase( st.find( key ) );`
782 | - Removes only one occurrence of the specified key if present.
783 | - Average Time Complexity: 𝓞(1)
784 | - Worst Time Complexity: 𝓞(n)
785 |
786 | - `st.find( key );`
787 | - Returns an iterator pointing to the key. If the key is not present, returns `st.end()`.
788 | - Average Time Complexity: 𝓞(1)
789 | - Worst Time Complexity: 𝓞(n)
790 |
791 | - `st.count( key ) ;`
792 | - Returns the frequency of the specified key.
793 | - Average Time Complexity: 𝓞(1)
794 | - Worst Time Complexity: 𝓞(n)
795 |
796 | - `st.size();`
797 | - Returns the size of the multiset.
798 | - Time Complexity: 𝓞(1)
799 |
800 | - `st.empty();`
801 | - Returns true if the multiset is empty, else false.
802 | - Time Complexity: 𝓞(1)
803 |
804 | - `st.clear();`
805 | - Removes all multiset elements.
806 | - Time Complexity: 𝓞(n)
807 |
808 | ## Accessing multiset elements
809 | Since it doesn't provide indexing, we cannot directly access any element. You can traverse the multiset using the following loop:
810 |
811 | ```cpp
812 | for (auto x : st) {
813 | cout << x << " "; // prints each key in ascending order
814 | }
815 | ```
816 |
817 | *Lets understand by sample program*
818 |
819 | ```cpp
820 | #include
821 | using namespace std;
822 |
823 | int main() {
824 | unordered_multiset st;
825 |
826 | // Inserting data into the multiset
827 | st.insert(1); // inserts 1 into multiset
828 | st.insert(1); // inserts 1 into multiset
829 | st.insert(1); // inserts 1 into multiset
830 | st.insert(5); // inserts 5 into multiset
831 | st.insert(10); // inserts 10 into multiset
832 | st.insert(15); // inserts 15 into multiset
833 | st.insert(5); // inserts 5 into multiset
834 |
835 | // Checking if 100 is present in the multiset
836 | if (st.find(100) != st.end()) {
837 | cout << "Key is present ";
838 | } else {
839 | cout << "Key is not present "; // prints key is not present
840 | }
841 |
842 | // Checking if 1 is present in the multiset
843 | if (st.find(1) != st.end()) {
844 | cout << "Key is present ";
845 | } else {
846 | cout << "Key is not present ";
847 | }
848 |
849 | // Counting occurrences of 1 in the multiset
850 | cout << st.count(1); // prints 3
851 |
852 | // Removing one occurrence of 1 from the multiset
853 | st.erase(st.find(1));
854 |
855 | // Counting occurrences of 1 in the multiset after removal
856 | cout << st.count(1); // prints 2
857 |
858 | // Removing all occurrences of 1 from the multiset
859 | st.erase(1);
860 |
861 | // Counting occurrences of 1 in the multiset after removal
862 | cout << st.count(1); // prints 0
863 |
864 | // Printing the size of the multiset
865 | cout << st.size() << " "; // prints 4
866 |
867 | // Printing whether the multiset is empty or not
868 | cout << st.empty(); // prints false | 0
869 |
870 | // Removing all elements from the multiset
871 | st.clear(); // removes all elements.
872 |
873 | return 0;
874 | }
875 | ```
876 |
877 | ---
878 |
879 | # MAPS IN C++
880 |
881 | - A map is a data structure that stores elements in a mapped fashion. Each element has a key value and a mapped value, effectively storing `key-value pairs`.
882 | - Keys are unique and sorted in ascending order by default.
883 | - Implemented using `Binary Search Trees` (Red-Black Trees).
884 |
885 | ## Declaring a Map
886 |
887 | ### Syntax
888 |
889 | ```cpp
890 | map mp; keys are unique and sorted in ASC
891 | ```
892 |
893 | ### Examples
894 |
895 | - `map mp;` map of int as key and int as value
896 | - `map mp;` map of int as key and String as value
897 | - `map> mp;` map of int as key and vector as value
898 | - `map> mp;` map of String as key and vector as value
899 |
900 |
901 | Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.
902 |
903 |
904 | ## Important functions
905 |
906 | - `mp[key] = value;`
907 | Inserts key-value pair if not present. If present, updates the key with the current value.
908 | TC - O(logn)
909 |
910 | - `int a = mp[key];`
911 | Returns the value at the specified key. If key is not present, it returns the default value and adds key->default_value into the map.
912 | TC - O(logn)
913 |
914 | - `mp.insert({key, value});`
915 | Inserts a new key-value pair if the key is not present. If present, it won't add this key-value pair and won't update.
916 |
917 | - `mp.insert(make_pair(key, value));`
918 | Inserts a new key-value pair if the key is not present. If present, it won't add this key-value pair and won't update.
919 |
920 | - `mp.insert(pair(5, 7));`
921 | Inserts a new key-value pair if the key is not present. If present, it won't add this key-value pair and won't update.
922 |
923 | - `mp.erase(key);`
924 | Removes the specified entry with the specified key if present.
925 | TC - O(logn)
926 |
927 | - `mp.count(key);`
928 | Returns the frequency of the key (0 or 1).
929 | TC - O(logn)
930 |
931 | - `mp.find(key);`
932 | Returns the iterator pointing to the key if present, else returns mp.end().
933 | TC - O(logn)
934 |
935 | - `mp.clear();`
936 | Removes all entries from the map.
937 | TC - O(n)
938 |
939 | - `mp.size();`
940 | Returns the size of the map.
941 | TC - O(1)
942 |
943 | - `mp.empty();`
944 | Returns true if the map is empty, else false.
945 | TC - O(1)
946 |
947 | ## Traversing through map
948 |
949 | ### First way
950 | ```cpp
951 | for(auto ele : mp){
952 | cout << ele.first << " " << ele.second << "\n";
953 | }
954 | ```
955 | ### Second way
956 | ```cpp
957 | for(auto it = mp.begin() ; it != mp.end() ; it++){
958 | cout << it->first << " " << it->second << "\n";
959 | }
960 | ```
961 | ### Third way
962 | ```cpp
963 | for(auto it = mp.rbegin() ; it != mp.rend() ; it++){
964 | cout << it->first << " " << it->second << "\n";
965 | }
966 | ```
967 |
968 | *Lets understand by sample program*
969 |
970 | ```cpp
971 | #include
972 | using namespace std;
973 |
974 | int main() {
975 | // your code goes here
976 | map mp; // creates an empty map ---> []
977 | mp[5] = 10; // since key = 5 is not present, adds the entry with key as 5 and value as 10 ---> [5->10]
978 | mp[5] = 9; // key = 5 is present hence it updates with current value ---> [5->9]
979 | mp.insert(pair(3, 7)); // since key = 3 is not present, adds the entry 3->7 ----> [3->7, 5->9]
980 | mp.insert(pair(5, 8)); // since key = 5 is present, it won't add and update the entry. ---> [3->7, 5->9]
981 | mp.insert(make_pair(8, 1)); // since key = 8 is not present, adds the entry 8->1 ----> [3->7, 5->9, 8->1]
982 | mp.insert(make_pair(8, 2)); // since key = 8 is present, it won't add and update the entry. ---> [3->7, 5->9, 8->1]
983 | mp.insert({6, 11}); // since key = 6 is not present, adds the entry 6->11 ----> [3->7, 5->9, 6->11, 8->1]
984 | mp.insert({6, 99}); // since key = 6 is present, it won't add and update the entry. ---> [3->7, 5->9, 6->11, 8->1]
985 | cout << "size is " << mp.size() << "\n"; // prints 4
986 | mp.erase(5); // removes entry whose key = 5 i.e 5->9. ---> [3->7, 6->11, 8->1]
987 | mp.erase(100); // removes entry whose key = 100, but there is no entry matching this. So nothing will happen. ---> [3->7, 6->11, 8->1]
988 | cout << "value at key = 8 is " << mp[8] << "\n"; // prints the value at key = 8 i.e 1
989 | cout << "value at key = 99 is " << mp[99] << "\n"; // since there is no key = 99 it adds entry 99->0 (0 is the default value) and prints 0. ---> [3->7, 6->11, 8->1, 99->0]
990 | cout << "Frequency of entry whose key = 3 is " << mp.count(3) << "\n";
991 |
992 | if (mp.find(12) != mp.end())
993 | cout << "Key = 12 is present \n";
994 | else
995 | cout << "key = 12 is not present\n";
996 |
997 | if (mp.find(99) != mp.end())
998 | cout << "Key = 99 is present \n";
999 | else
1000 | cout << "key = 99 is not present\n";
1001 |
1002 | // printing map
1003 | cout << "map entries are: \n";
1004 | for (auto it = mp.begin(); it != mp.end(); it++) {
1005 | cout << it->first << "->" << it->second << "\n";
1006 | }
1007 | return 0;
1008 | }
1009 | ```
1010 |
1011 | ---
1012 |
1013 | # UNORDERED MAPS IN C++
1014 |
1015 | - An `unordered_map` is a data structure that stores elements in a mapped fashion. Each element has a key value and a mapped value. Essentially, it stores key-value pairs.
1016 | - Keys are unique and do not have a particular order.
1017 | - Implemented using `Hash tables`.
1018 |
1019 | ## Declaring map
1020 |
1021 | ### Syntax
1022 | ```cpp
1023 | unordered_map mp; // keys are unique and not have a particular order
1024 | ```
1025 |
1026 | ### Examples
1027 |
1028 | - `unordered_map mp;` unordered_map of int as key and int as value
1029 | - `unordered_map mp;` unordered_map of int as key and String as value
1030 | - `unordered_map> mp;` unordered_map of int as key and vector as value
1031 | - `unordered_map> mp;` unordered_map of String as key and vector as value
1032 |
1033 | Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.
1034 |
1035 |
1036 | ## Important Functions
1037 |
1038 | - `mp[key] = value;`
1039 | Inserts key->value pair if not present. If present, updates the key with the current value.
1040 | - Average TC: O(1)
1041 | - Worst TC: O(n)
1042 |
1043 | - `int a = mp[key];`
1044 | Returns the value at the specified key. If the key is not present, it returns the default value and adds key->default_value into the map.
1045 | - Average TC: O(1)
1046 | - Worst TC: O(n)
1047 |
1048 | - `mp.insert(make_pair(key, value));`
1049 | Inserts a new key-value pair if the key is not present. If present, it won't add this key-value pair and won't update.
1050 | - Average TC: O(1)
1051 | - Worst TC: O(n)
1052 |
1053 | - `mp.insert({key, value});`
1054 | Inserts a new key-value pair if the key is not present. If present, it won't add this key-value pair and won't update.
1055 | - Average TC: O(1)
1056 | - Worst TC: O(n)
1057 |
1058 | - `mp.insert(pair(5, 7));`
1059 | Inserts a new key-value pair if the key is not present. If present, it won't add this key-value pair and won't update.
1060 | - Average TC: O(1)
1061 | - Worst TC: O(n)
1062 |
1063 | - `mp.erase(key);`
1064 | Removes the specified entry with the specified key if present.
1065 | - Average TC: O(1)
1066 | - Worst TC: O(n)
1067 |
1068 | - `mp.count(key);`
1069 | Returns the frequency of the key (0 or 1).
1070 | - Average TC: O(1)
1071 | - Worst TC: O(n)
1072 |
1073 | - `mp.find(key);`
1074 | Returns the position of the key if present, else returns `mp.end()`.
1075 | - Average TC: O(1)
1076 | - Worst TC: O(n)
1077 |
1078 | - `mp.clear();`
1079 | Removes all entries from the map.
1080 | - TC: O(n)
1081 |
1082 | - `mp.size();`
1083 | Returns the size of the map.
1084 | - TC: O(1)
1085 |
1086 | - `mp.empty();`
1087 | Returns true if the map is empty, else false.
1088 | - TC: 𝓞(1)
1089 |
1090 | ## Traversing through Map
1091 |
1092 | ### First Way
1093 | ```cpp
1094 | for(auto ele : mp){
1095 | cout << ele.first << " " << ele.second << "\n";
1096 | }
1097 | ```
1098 | ### Second way
1099 | ```cpp
1100 | for(auto it = mp.begin() ; it != mp.end() ; it++){
1101 | cout << it->first << " " << it->second << "\n";
1102 | }
1103 | ```
1104 | ### Third way
1105 | ```cpp
1106 | for(auto it = mp.rbegin() ; it != mp.rend() ; it++){
1107 | cout << it->first << " " << it->second << "\n";
1108 | }
1109 | ```
1110 |
1111 | *Lets understand by sample program*
1112 |
1113 | ```cpp
1114 | #include
1115 | using namespace std;
1116 |
1117 | int main() {
1118 | unordered_map mp; // creates an empty map ---> []
1119 | mp[5] = 10; // since key = 5 is not present, adds the entry with key as 5 and value as 10 ---> [5->10]
1120 | mp[5] = 9; // key = 5 is present hence it updates with the current value ---> [5->9]
1121 | mp.insert(pair(3, 7)); // since k = 3 is not present adds the entry 3->7 ----> [5->9, 3->7] (Note: order is not guaranteed)
1122 | mp.insert(pair(5, 8)); // since key = 5 is present it won't add and update the entry. ---> [3->7, 5->9] (Note: order is not guaranteed)
1123 | mp.insert(make_pair(8, 1)); // since key = 8 is not present adds the entry 8->1 ----> [3->7, 5->9, 8->1] (Note: order is not guaranteed)
1124 | mp.insert(make_pair(8, 2)); // since key = 8 is present it won't add and update the entry. ---> [3->7, 5->9, 8->1] (Note: order is not guaranteed)
1125 | mp.insert({6, 11}); // since key = 6 is not present adds the entry 6->11 ----> [3->7, 5->9, 6->11, 8->1] (Note: order is not guaranteed)
1126 | mp.insert({6, 99}); // since key = 6 is present it won't add and update the entry. ---> [6->11, 8->1, 5->9, 3->7] (Note: order is not guaranteed)
1127 | cout << "size is " << mp.size() << "\n"; // prints 4
1128 | mp.erase(5); // removes entry whose key = 5 i.e 5->9. ---> [3->7, 8->1, 6->11] (Note: order is not guaranteed)
1129 | mp.erase(100); // removes entry whose key = 100, but there is no entry matching this. So nothing will happen. ---> [3->7, 6->11, 8->1] (Note: order is not guaranteed)
1130 | cout << "value at key = 8 is " << mp[8] << "\n"; // prints the value at key = 8 i.e 1
1131 | cout << "value at key = 99 is " << mp[99] << "\n"; // since there is no key = 99 it adds entry 99->0 (0 is the default value) and prints 0. ---> [3->7, 6->11, 8->1, 99->0] (Note: order is not guaranteed)
1132 | cout << "Frequency of entry whose key = 3 is " << mp.count(3) << "\n";
1133 |
1134 | if(mp.find(12) != mp.end()) cout << "Key = 12 is present \n";
1135 | else cout << "key = 12 is not present\n";
1136 |
1137 | if(mp.find(99) != mp.end()) cout << "Key = 99 is present \n";
1138 | else cout << "key = 99 is not present\n";
1139 |
1140 | // printing map
1141 | cout << "map entries are: \n" ;
1142 | for(auto it = mp.begin() ; it != mp.end() ; it++){
1143 | cout << it->first << "->" << it->second << "\n";
1144 | }
1145 | return 0;
1146 | }
1147 | ```
1148 | ---
1149 |
1150 |
1151 | # Multimaps in C++
1152 |
1153 | - A `multimap` is a data structure that stores elements in a mapped fashion. Each element has a key value and a mapped value, essentially storing key-value pairs.
1154 | - The multimap is similar to a map, with the addition that multiple elements can have the same keys.
1155 | - Keys are sorted in ascending order by default.
1156 | - Implemented using `Binary Search Trees` (Red-Black Trees).
1157 |
1158 | ## Declaring Multimap
1159 |
1160 | ### Syntax:
1161 |
1162 | ```cpp
1163 | multimap mp; // keys are sorted in ascending order
1164 | ```
1165 |
1166 | ### Examples
1167 |
1168 | - `multimap mp;` multimap of int's
1169 | - `multimap mp;` multimap of strings
1170 | - `multimap> mp;` multimap of vectors
1171 | - `multimap> mp;` multimap of vectors
1172 |
1173 |
1174 | Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.
1175 |
1176 | ## Important functions
1177 |
1178 | ```cpp
1179 | mp.insert(make_pair(key, value)); // inserts new key-value pair TC - O(logn)
1180 | mp.insert({key, value}); // inserts new key-value pair TC - O(logn)
1181 | mp.insert(pair(5, 7)); // inserts new key-value pair TC - O(logn)
1182 | mp.erase(key); // removes all occurrences of entries with specified key if present. TC - O(logn)
1183 | mp.erase(mp.find(key)); // removes one occurrence of entry with specified key if present. TC - O(logn)
1184 | mp.count(key); // returns frequency of key TC - O(logn)
1185 | mp.find(key); // returns pos of key if present. else return mp.end() TC - O(logn)
1186 | mp.clear(); // removes all entries from the map TC - O(n)
1187 | mp.size(); // returns the size of map TC - O(1)
1188 | mp.empty(); // returns true if map is empty else false TC - 𝓞(1)
1189 | ```
1190 |
1191 | ## Traversing through map
1192 |
1193 | ### First way
1194 | ```cpp
1195 | for(auto ele : mp){
1196 | cout << ele.first << " " << ele.second << "\n";
1197 | }
1198 | ```
1199 |
1200 | ### Second way
1201 | ```cpp
1202 | for(auto it = mp.begin(); it != mp.end(); it++){
1203 | cout << it->first << " " << it->second << "\n";
1204 | }
1205 | ```
1206 |
1207 | ### Third way
1208 | ```cpp
1209 | for(auto it = mp.rbegin(); it != mp.rend(); it++){
1210 | cout << it->first << " " << it->second << "\n";
1211 | }
1212 | ```
1213 |
1214 | *Lets understand by sample program*
1215 |
1216 | ```cpp
1217 | #include
1218 | using namespace std;
1219 |
1220 | int main() {
1221 | // your code goes here
1222 | multimap mp; // creates an empty map ---> []
1223 |
1224 | mp.insert(pair(3, 7)); // adds the entry 3->7 ----> [3->7]
1225 | mp.insert(pair(5, 8)); // adds the entry 5->8---> [3->7, 5->8]
1226 | mp.insert(make_pair(8, 1)); // adds the entry 8->1.---> [3->7, 5->8, 8->1]
1227 | mp.insert(make_pair(8, 2)); // adds the entry 8->2.---> [3->7, 5->8, 8->1, 8->2]
1228 | mp.insert({6, 11}); // adds the entry 6->11.---> [3->7, 5->8, 6->11, 8->1, 8->2]
1229 | mp.insert({6, 99}); // adds the entry 6->11.---> [3->7, 5->8, 6->11, 6->99, 8->1, 8->2]
1230 | mp.insert({6, 15}); // adds the entry 6->15.---> [3->7, 5->8, 6->11, 6->99, 6->15, 8->1, 8->2]
1231 | cout << "size is " << mp.size() << "\n"; // prints 7
1232 | cout << "Frequency of entry whose key = 6 is " << mp.count(6) << "\n";
1233 | auto it = mp.begin();
1234 | while(it != mp.end()){
1235 | if(it->first == 6 && it->second == 11) break;
1236 | it++;
1237 | }
1238 | mp.erase(it); // removes entry that 'it' points i.e 6->11. ---> [3->7, 5->8, 6->99, 6->15, 8->1, 8->2]
1239 | cout << "Frequency of entry whose key = 6 after removing one occurrence is " << mp.count(6) << "\n";
1240 | mp.erase(6); // removes all occurrences of entry's whose keys = 6 ---> [3->7, 5->8, 8->1, 8->2]
1241 | cout << "Frequency of entry whose key = 6 after removing all occurrences is " << mp.count(6) << "\n";
1242 | mp.erase(100); // since there is no key = 100, nothing will be removed ---> [3->7, 5->8, 8->1, 8->2]
1243 |
1244 | if(mp.find(12) != mp.end()) cout << "Key = 12 is present \n";
1245 | else cout << "key = 12 is not present\n";
1246 |
1247 | if(mp.find(8) != mp.end()) cout << "Key = 8 is present \n";
1248 | else cout << "key = 99 is not present\n";
1249 |
1250 | // printing map
1251 | cout << "map entry's are: \n" ;
1252 | for(auto it = mp.begin() ; it != mp.end() ; it++){
1253 | cout << it->first << "->" << it->second << "\n";
1254 | }
1255 | return 0;
1256 | }
1257 | ```
1258 |
1259 | # UNORDERED MULTIMAPS IN C++
1260 |
1261 | - An `unordered_multimap` is a data structure that stores elements in a mapped fashion. Each element has a key value and mapped value, essentially storing key-value pairs.
1262 | - Multimap is similar to a map with the addition that `multiple elements can have the same keys`.
1263 | - Keys in an `unordered_multimap` are not sorted, meaning there is no particular order.
1264 | - Implemented using `Hash Tables`.
1265 |
1266 | ## Declaring multimap
1267 |
1268 | ### Syntax
1269 |
1270 | ```cpp
1271 | unordered_multimap mp; // keys are not sorted i.e no particular order
1272 | ```
1273 |
1274 | ### Examples
1275 |
1276 | - `unordered_multimap mp;` unordered_multimap of int as key and int as value
1277 | - `unordered_multimap mp;` unordered_multimap of int as key and String as value
1278 | - `unordered_multimap> mp;` unordered_multimap of int as key and vector as value
1279 | - `unordered_multimap> mp;` unordered_multimap of String as key and vector as value
1280 |
1281 |
1282 | Note: Similar syntax for `char`, `long long int`, `float`, `double`, `long double`, and some other data types, including user-defined data types.
1283 |
1284 |
1285 | ## Important Functions
1286 |
1287 | - `mp.insert(make_pair(key, value));`
1288 | Inserts a new key-value pair.
1289 | - Average Time Complexity: O(1)
1290 | - Worst Time Complexity: O(n)
1291 |
1292 | - `mp.insert({key, value});`
1293 | Inserts a new key-value pair.
1294 | - Average Time Complexity: O(1)
1295 | - Worst Time Complexity: O(n)
1296 |
1297 | - `mp.insert(pair(5, 7));`
1298 | Inserts a new key-value pair.
1299 | - Average Time Complexity: O(1)
1300 | - Worst Time Complexity: O(n)
1301 |
1302 | - `mp.erase(key);`
1303 | Removes all occurrences of entries with the specified key if present.
1304 | - Average Time Complexity: O(1)
1305 | - Worst Time Complexity: O(n)
1306 |
1307 | - `mp.erase(mp.find(key));`
1308 | Removes one occurrence of the entry with the specified key if present.
1309 | - Average Time Complexity: O(1)
1310 | - Worst Time Complexity: O(n)
1311 |
1312 | - `mp.count(key);`
1313 | Returns the frequency of the key.
1314 | - Average Time Complexity: O(1)
1315 | - Worst Time Complexity: O(n)
1316 |
1317 | - `mp.find(key);`
1318 | Returns the position of the key if present, otherwise returns `mp.end()`.
1319 | - Average Time Complexity: O(1)
1320 | - Worst Time Complexity: O(n)
1321 |
1322 | - `mp.clear();`
1323 | Removes all entries from the map.
1324 | - Time Complexity: O(n)
1325 |
1326 | - `mp.size();`
1327 | Returns the size of the map.
1328 | - Time Complexity: O(1)
1329 |
1330 | - `mp.empty();`
1331 | Returns true if the map is empty; otherwise, false.
1332 | - Time Complexity: 𝓞(1)
1333 |
1334 | ## Traversing through unordered_multimap
1335 |
1336 | ### First Way
1337 | ```cpp
1338 | for(auto ele : mp){
1339 | cout << ele.first << " " << ele.second << "\n";
1340 | }
1341 | ```
1342 |
1343 | ### Second way
1344 | ```cpp
1345 | for(auto it = mp.begin() ; it != mp.end() ; it++){
1346 | cout << it->first << " " << it->second << "\n";
1347 | }
1348 | ```
1349 |
1350 | ### Third way
1351 | ```cpp
1352 | for(auto it = mp.rbegin() ; it != mp.rend() ; it++){
1353 | cout << it->first << " " << it->second << "\n";
1354 | }
1355 | ```
1356 |
1357 | *Lets understand by sample program*
1358 |
1359 | ```cpp
1360 | #include
1361 | using namespace std;
1362 |
1363 | int main() {
1364 | // your code goes here
1365 | unordered_multimap mp; // creates an empty map ---> []
1366 |
1367 | mp.insert(pair(3, 7)); // adds the entry 3->7 ----> [3->7] (Note: Order of elements is not expected)
1368 | mp.insert(pair(5, 8)); // adds the entry 5->8---> [3->7, 5->8] (Note: Order of elements is not expected)
1369 | mp.insert(make_pair(8, 1)); // adds the entry 8->1.---> [3->7, 5->8, 8->1] (Note: Order of elements is not expected)
1370 | mp.insert(make_pair(8, 2)); // adds the entry 8->2.---> [3->7, 5->8, 8->1, 8->2] (Note: Order of elements is not expected)
1371 | mp.insert({6, 11}); // adds the entry 6->11.---> [3->7, 5->8, 6->11, 8->1, 8->2] (Note: Order of elements is not expected)
1372 | mp.insert({6, 99}); // adds the entry 6->11.---> [3->7, 5->8, 6->11, 6->99, 8->1, 8->2] (Note: Order of elements is not expected)
1373 | mp.insert({6, 15}); // adds the entry 6->15.---> [3->7, 5->8, 6->11, 6->99, 6->15, 8->1, 8->2] (Note: Order of elements is not expected)
1374 | cout << "size is " << mp.size() << "\n"; // prints 7
1375 | cout << "Frequency of entry whose key = 6 is " << mp.count(6) << "\n";
1376 | auto it = mp.begin();
1377 | while(it != mp.end()){
1378 | if(it->second == 11) break;
1379 | it++;
1380 | }
1381 | mp.erase(it); // removes the entry that 'it' points i.e 6->11. ---> [3->7, 5->8, 6->99, 6->15, 8->1, 8->2]
1382 | cout << "Frequency of entry whose key = 6 after removing one occurrence is " << mp.count(6) << "\n";
1383 | mp.erase(6); // removes all occurrences of entries whose keys = 6 ---> [3->7, 5->8, 8->1, 8->2]
1384 | cout << "Frequency of entry whose key = 6 after removing all occurrences is " << mp.count(6) << "\n";
1385 | mp.erase(100); // since there is no key = 100, nothing will be removed ---> [3->7, 5->8, 8->1, 8->2]
1386 |
1387 | if(mp.find(12) != mp.end()) cout << "Key = 12 is present \n";
1388 | else cout << "key = 12 is not present\n";
1389 |
1390 | if(mp.find(8) != mp.end()) cout << "Key = 8 is present \n";
1391 | else cout << "key = 99 is not present\n";
1392 |
1393 | // printing map
1394 | cout << "map entry's are: \n" ;
1395 | for(auto it = mp.begin() ; it != mp.end() ; it++){
1396 | cout << it->first << "->" << it->second << "\n";
1397 | }
1398 | return 0;
1399 | }
1400 | ```
1401 |
1402 |
1403 | ---
1404 |
1405 | ### Thanks for Reading
1406 |
1407 |
1408 |
1409 | ---
1410 |
1411 | ###
1412 |
1413 | #### Join us for all the latest offcampus job updates, webinar, hackathons, resume review and a lot more :heart::heart:
1414 |
1415 |
1432 |
1433 | ###
1434 |
1435 | ---
1436 |
--------------------------------------------------------------------------------
/assets/images/CPPSTL.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aman0046/CPP-STL-Library/fb21de8a6d3663ddf2f51279c12ae00e5895b7d1/assets/images/CPPSTL.png
--------------------------------------------------------------------------------
/assets/images/save.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aman0046/CPP-STL-Library/fb21de8a6d3663ddf2f51279c12ae00e5895b7d1/assets/images/save.png
--------------------------------------------------------------------------------
/assets/images/test:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------