0% found this document useful (0 votes)
14 views

Lecture 6

Uploaded by

nerf quta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture 6

Uploaded by

nerf quta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Set, Map & Stack

Greetings, dear students. I hope you are all alright and well <3 today we are going to discuss 3
different collection types, namely: set, map and stack. Let’s start with Set.
Sets are a type of associative container in which each element has to be unique because the
value of the element identifies it. Set saves its values sorting in ascending order by default.
1. set <data_type> set_name; // syntax of declaring set variable
2. set<int> val; // creating a set of type integer
3. set<int, greater<int> > val2; // creating a set of type integer with decreasing order
Adding values to set variable:
1. set<int> val = {6, 10, 5, 1}; // creating a set of type int and assigning values to it
2. val.insert(8); // adding values later with built-in function of set

Properties of Set

1. Storing order – The set stores the elements in sorted order.


2. Values Characteristics – All the elements in a set have unique values.
3. Values Nature – The value of the element cannot be modified once it is added to the
set, though it is possible to remove and then add the modified value of that element.
Thus, the values are immutable.
4. Search Technique – Sets follow the Binary search tree implementation.
5. Arranging order – The values in a set are unindexed.
Working with sets may be difficult for some, because we can not directly touch values saved in it
and we have no choice but to use set built-in functions, such as:
 size() – Returns the number of elements in the set.
 max_size() – Returns the maximum number of elements that the set can hold.
 empty() – Returns whether the set is empty.
 Insert(value) – Inserts value in the set.
 Erase(value) – erases value from the set
 Find(value) – returns iterator pointing to the value
 Clear() – clears the whole set
 Count(value) – return 0 or 1 based on if value is present in the set
This was all about the sets for now. Now it is time for Map.
Maps are associative containers that store elements in a mapped fashion. Each element has a
key value and a mapped value. No two mapped values can have the same key values. In arrays,
we had indexes to access saved values, in sets we had just values and no indexes, but in Map
we have Keys, which can be anything we want them to be and values associated with these
keys.
Map is defined inside the <map> header file. Now we can look at the syntax:
1. #include <iostream>
2. #include <map>
3. #include <string>
4. using namespace std;
5. int main(){
6. // Create a map of strings to integers
7. map<string, int> mp;
8. // Insert some values into the map
9. mp["one"] = 1;
10. mp["two"] = 2;
11. mp["three"] = 3;
as set and other collections, map also has many built-in functions. Here are some of them:
1. #include <iostream>
2. #include <map>
3. #include <string>
4. using namespace std;
5. int main(){
6. map <int, int> map1;
7. for (int i=1;i<10;i++){
8. map1[i]=i*i;
9. }
10. for (int i=1;i<10;i++){
11. cout << map1[i] << " ";
12. }
13. cout << endl;
14. cout << map1.size() << endl; // checks current size of map
15. cout << map1.max_size() << endl; // checks max size of map
16. cout << map1.empty() << endl; // checks if map is empty
17. map1.insert(pair <int, int> (10, 100)); // inserts new key and value as a pair in the
map
18. map1.erase(10); // erases key-value which is equal to value in parenthesis
19. map1.clear(); // clears the whole map
20. }
And now it is time to explain the last new topic for today, which is called stack. Stacks are
type of containers, which has following working structure: “Last In First Out” (LIFO), this
means, that if you have 10 values saved in stack, you only can take out the last 10th value,
then 9th, then 8th and so on, BUT you cannot skip positions. (you cannot take out 2nd when
you have 5 elements in stack). Of course, we also have built-in functions for stacks, which
are:
empty(), size() – which function in the same way as they did in previous collection data
types.
top() – return the value of top most element in stack
push(value) – pushes value on top of stack
pop() – removes the top most value from stack
now, we can look at the syntax and learn how to use stacks: firstly, you have to import the
<stack> library with #include <stack>, after which you can do the following:
1. #include <iostream>
2. #include <stack>
3. using namespace std;
4. int main() {
5. stack<int> stack1; // defining the stack
6. stack1.push(21);// The values pushed in the stack should be of the same data
which is written during declaration of stack
7. int num=0;
8. stack.push(num); // pushing meaning from variables into the stack
9. stack.pop(); // popping out the value from the stack, the top value
10. while (!stack.empty()) { // checking if stack is empty
11. cout << stack.top() <<" ";
12. stack.pop();
13. }
14. }
This was all about the theory, now let’s move on to the practical part and do some exercises to
understand new knowledge better.

You might also like