Professional Documents
Culture Documents
Session #2 ( STL's )
Session #2 ( STL's )
Session #2 ( STL's )
A container is a holder object that stores a collection of other objects (its elements).
They are implemented as class templates, which allows great flexibility in the types
supported as elements.
The container manages the storage space for its elements and provides member
functions to access them, either directly or through iterators
Itrators → ( reference objects with similar properties to pointers ).
Containers
Types Containers
1. Sequence containers
2. Associative containers
3. Unordered associative containers → ( Search With Yourself )
4. Container adapters
Sequence containers
Sequence containers implement data structures that can be accessed sequentially.
Associative containers implement sorted data structures that can be quickly searched
( O ( log n ) complexity → ( BST Algorithm )
● Stack : Adapts a container to provide stack ( LIFO data structure ) → ( Last In First Out )
● Queue : Adapts a container to provide queue ( FIFO data structure ) → ( First In First Out )
● front()
● back()
● size()
● empty()
● insert()
● erase()
● push_back()
● pop_back()
Note That Vectors allow random access just like normal arrays
● sort() v[0], v[1], v[2], …... , v[v.size()-1]
Let’s Practice
Problem 1
● .push()
adds an element to the top of the stack.
● .pop()
removes the top element.
Important To Know That We Cannot
Access Any Element at The Middle
Stack
.top()
returns the top element.
.size()
Returns the number of elements.
.empty()
returns whether the stack is empty.
Stack
Stack Declaration Example
What Is OutPut
Brainstorm With Me !
What Is OutPut
Answer
Let’s Practice
Problem 3
.front()
Return Front element
.back()
Return rear element
.size()
Returns the number of elements.
.empty()
returns whether the queue is empty.
Brainstorm With Me !
What Is OutPut
Let’s Practice
Problem 4
Qotb ranks students according to their intelligence. N
students will enter the magical bus in a queue, but the
queue to bus has some magical rules, when one student
come in the back of the queue the mentors check
whether the first one in the queue has the same rank of
him or no, if the last rank matches the first one, the first
one will enter the bus. ranks of students according to
Koko are "smart", "geek", and "nerd", you should print the
ranks of the students that will stand in the queue from
the beginning to the end after that the N students came to
stand in the queue.
Solution
Problem 4
Queue Vs Stack
Deque
Deque can insert and delete from both ends
Deque Declaration
Deque
.push_back()
Push element to the back of deque.
.push_front()
Push element to the front of deque.
.pop_back()
Pop element from the back of deque.
.pop_front()
Pop element from the front of deque.
What Is OutPut
Deque
.size()
Return the size of deque.
.empty()
Return a bool, true if deque is
empty and false otherwise.
.clear()
Remove all the elements from
deque.
What Is OutPut
Let’s Practice
Problem 5
Important To Know That Using this declaration priority queue Will store the elements in a way
that makes the top element is the smallest one.
Priority Queue
.push()
Add an element to the priority queue.
.top()
Get top element of the priority queue.
.pop()
Remove top element of the priority
queue.
What Is OutPut
Let’s Practice
Problem 6
Thousands of years ago, a pharaoh has got new words as a
present, since you
are the smartest engineer in the country, he immediately asked
for you to help him, he will give you the words one by one and
you should put them in a book
( he give you the word when he tell you the secret number 1 ),
easy task right ?. unfortunately the pharaoh wants to test your
focus level during the job, so from time to time he will ask you to
tell him which word is lexicographically the biggest among all
the words he gave you until the moment he asked you in when
he miss the secret number .
Solution
Problem 6
Binary Search Tree ( BST ) - [ Just Brief ]
What is BST ?
● Algorithm to search for a key in a given Binary Search Tree:
● Let’s say we want to search for the number X, We start at the
root. Then:
● We compare the value to be searched with the value of the root.
● If it’s equal we are done with the search if it’s smaller we know
that we need to go to the left subtree because in a binary search
tree all the elements in the left subtree are smaller and all the
elements in the right subtree are larger.
● Repeat the above step till no more traversal is possible
● If at any iteration, key is found, return True. Else False.
Set
● Sorted Data Structure & Does Not Repeated Values
Set can sort in both orders, Ascending and descending.
● Increase Set
.insert () : to add element in the set.
Insertion takes O ( log ( n ) ). → ( Because its built with BST Algorithm )
● Decrease Set
Remember That ( Iterator )
● To access set elements, you need an iterator.
● An iterator is an object that points/refers to an element of a sequence.
● The end of the sequence is one past the last element.
● * → To Show element
● & → To Show Reference || Address
Let’s Practice
Problem 7
Fred sent his daughter to the shop to buy some
groceries, but he gave her a very weird shopping list,
she found that many items in this list are repeated.
She wants you to help him create a new list, where
each item is unique.
Solution
Problem 7
MultiSet
● Sorted Data Structure & Can Repeat Values and Count Them
MultiSet can sort in both orders, Ascending and descending.
● Increase MultiSet
● Decrease MultiSet
MultiSet
What Is OutPut
Map
● You can use any data type for the key or value.
● Declare Map
Map
● Assign a value to the key. ● .size() : shows how many elements in the map.
● To show the value of a certain key. ● .clear() : clear all elements in the map
Let’s Practice
Problem 8
Fred is the judge of a tennis tournament. He was a
given a list of n names, the names of the players who
won a match. The player who won the greatest number
of matches wins the whole tournament. Print the name
of the player who won the tournament, and how many
times Zeyad won matches.
Solution
Problem 8
Zeyad Not Equal zeyad , This Your Fault Right ?
InCase Not Shater
OutPut
Min_element ()
OutPut
Accumulate ()
OutPut
Count ()
OutPut
Find ()
OutPut
Binary Search ()
Search if the element is found return 1. Otherwise, returns 0
( Note That Elements must be sorted )
Complexity : O ( Log 2 ( n ) )
OutPut
Lower_Bound ()
returns first number bigger than or equal to x
( Note That Elements must be sorted )
Complexity : O ( Log 2 ( n ) )
OutPut
Upper_Bound ()
returns first number bigger than x
( Note That Elements must be sorted )
Complexity : O ( Log 2 ( n ) )
OutPut
Erase ()
OutPut
Nth_element ()
NOT SORTING. Puts all the elements less than n behind and all the elements bigger after.
Complexity : O ( N Log ( N ) )
OutPut
Is_sorted ()
OutPut
Sort ()
OutPut
Reverse ()
OutPut
Next_permutation ()
OutPut
Task Question