Week 5
Week 5
STLs
String
2
| String
3
| String
4
| String
5
| Example
7
Vector
8
| Vector
● Vectors vs arrays
● Why vectors ?
9
| 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
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.
20
Stack
● .top()
returns the top element.
● .size()
Returns the number of elements.
● .empty()
returns whether the stack is empty.
21
Stack declaration
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
● .Pop()=dequeue
Remove from front
● .push()=enqueue
Insert into back
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
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
.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
44
| Priority Queue
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;
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
60
| Multiset
● multiset<data_type> mset_name;
61
| Multiset
● .insert() : to add element in the
multiset. Insertion takes
O(log(n)).
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;
65
| Map
● Assign a value to the key.
66
| Map
● .size() : shows how many
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>
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>
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!