0% found this document useful (0 votes)
19 views74 pages

Week 5

Uploaded by

Mohamed Makaty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views74 pages

Week 5

Uploaded by

Mohamed Makaty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

WEEK #5

STLs
String

2
| String

● Strings vs character array


● Size of character array has to be allocated
statically but string is allocated dynamically.
● String has a number of functionalities which
allow manifold operations on strings.

3
| String

● strings Built-in Functions


○ s.empty()
○ s.find()
○ s.pop_back()
○ s.push_back()
○ s.insert( )
○ s.front()
○ s.back()
○ s.size()
● Strings allow random access just like character arrays
○ s[0], s[1], s[2], …... , s[s.size()-1]

4
| String

➢ How to input and print a string ?


➢ How to use string functionalities

5
| Example

Mario has a string S and he likes the letter ʻbʼ.


He calls a string perfect if more than half of
the characters in that string are ʻbʼ and it has
even length.
Can you help him find if the string is perfect
or not?
| Solution

7
Vector

8
| Vector

● Vectors vs arrays
● Why vectors ?

9
| vector

● How to initialize vector ?


● How to use its functionalities
| Vector

❖ Built-in Functions
➢ front()
➢ back()
➢ size()
➢ empty()
➢ insert()
➢ erase()
➢ push_back()
➢ pop_back()
➢ sort()
❖ vectors allow random access just like normal arrays
➢ v[0], v[1], v[2], …... , v[v.size()-1]

11
| Example

You are given Q queries.


Each query is a command starting with ʻaʼ , ʻpʼ or ʻdʼ.
- ʻaʼ you will be given an integer N and you have to
add N to your vector.

- ʻdʼ you have to delete the last element.

- ʻpʼ print the current vector elements.

12
| Solution

13
pair
| pair

● Why pairs ?
● Pair initialization
| Example
Mario has N college friends and he wants to store their names and grades.
He is a lazy boy so he asked you to help him.
| solution
Stack

18
Stack
Last In First Out

19
Stack

● .push()
adds an element to the top of the stack.
● .pop()
removes the top element.

Warning : we cannot access any element at the middle

20
Stack

● .top()
returns the top element.
● .size()
Returns the number of elements.
● .empty()
returns whether the stack is empty.

21
Stack declaration

stack < dataType > name ;

22
? Trace :

23
? Trace cont.

24
| Exercise:
Koko will be given a sequence of brackets ʻ{ʻ, ʻ}ʼ. His task is to check whether this
sequence results a regular brackets sequence or not, "{}", "{{}}" are regular bracket
sequences while "{{", "{}{", are not. print yes if it's a regular bracket sequence
otherwise print no.

25
Solution
Queue

27
Queue
First In First Out

Front ?
Back ? 28
Queue

queue< DataType > name;

● .Pop()=dequeue
Remove from front
● .push()=enqueue
Insert into back

Warning : we cannot access any element at the middle

29
Queue
● .front()
Return Front element
● .back()
Return rear element
● .size()
Returns the number of elements.
● .empty()
returns whether the queue is empty.
30
Queue declaration

queue < dataType > name ;

31
| Exercise:
Qotb ranks students according to their intelligence. N students will enter the
magical ACM bus in a queue, but the queue to ACM 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.

32
Solution
| queue vs stack

34
Deque

35
Deque

36
Deque

Deque can insert and delete from both ends


| 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.
38
| 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.

39
| Exercise
Given Q queries of three types.
- 1 x means add x to the end of the list.
- 2 y means add y to the start of the list.
- 3 means to delete the smallest of the first vs the
last number in the list.
Print the size of the list and its elements after all the
queries.

40
Solution
Priority Queue

42
| Priority Queue

43
| Priority Queue

Priority queue stores the elements in


a way that makes the top element
Is the biggest one.

44
| Priority Queue

Using this declaration priority queue


Will store the elements in a way that
makes the top element is the smallest
one.

45
| 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.

46
| Exercise

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,
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.

47
| Exercise

48
| Solution

49
Binary Search Trees
| Binary Search Tree
| Binary Search Tree
Set

53
| Set
● set<data_type> set_name;

● Set doesn't repeat elements.

● Set can sort in both orders,


Ascending and descending.

54
| Set
● .insert() : to add element in the
set.
● Insertion takes O(log(n)).

55
? 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.

56
| Set
● To declare an iterator
set<data_type>::iterator iterator_name;

57
? Exercise
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.

58
| Solution

● ( *it ) to show set elements.


Multiset

60
| Multiset
● multiset<data_type> mset_name;

● Just like Set, it can sort in both


orders, Ascending and
Descending but with repetitions.

61
| Multiset
● .insert() : to add element in the
multiset. Insertion takes
O(log(n)).

● Determine the elements in the


multiset after running this code.

62
| Multiset
● .count(): return frequency of an element x in the Multiset.
● .erase():
1. remove all elements up to element with value x
2. remove all elements with value x

63
Map
| Map
● map<key, value> map_name;

● You can use any data type for the


key or value.

65
| Map
● Assign a value to the key.

● To show the value of a certain


key.

66
| Map
● .size() : shows how many
elements in the map.

● .clear() : clear all elements in the


map

67
? Exercise
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.

68
? Exercise cnt.

69
| Solution
● Donʼt forget :
#include<map>
#include<string>

● Zeyad is not equal to zeyad

● To traverse on the map, you have to use


iterators.
map<key, value>::iterator iterator_name;
Lower & Upper Bound

71
? Lower & Upper Bound
● Lower Bound: return first element equal or bigger than x.
● Upper Bound: return first element bigger than x.
● The elements in the range should be sorted.
● Lower & upper bound function can be used in different STLs.
● #include<algorithm>

Let the elements in the range are : 5 6 7 7 6 5 5 6


After sorting: 5 5 5 6 6 6 7 7
Lower Bound for 6: element at position 3 ( value : 6 ).
Upper Bound for 6: element at position 6 ( value : 7 ).

72
| Lower & Upper Bound
● Lower Bound: lower_bound (ForwardIterator first, ForwardIterator last, val);
● Upper Bound: upper_bound (ForwardIterator first, ForwardIterator last, val);

73
THANK YOU!

You might also like