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

[DS 25] Lab 8 - Hashtable

This document outlines a lab on data structures focusing on hashtables and unordered maps in C++. It includes an agenda, explanations of unordered maps, their member functions, and two tasks: a letter counter program and an employee payroll system. The document provides code examples and instructions for implementing these tasks using STL hashtables.

Uploaded by

hahqg91
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

[DS 25] Lab 8 - Hashtable

This document outlines a lab on data structures focusing on hashtables and unordered maps in C++. It includes an agenda, explanations of unordered maps, their member functions, and two tasks: a letter counter program and an employee payroll system. The document provides code examples and instructions for implementing these tasks using STL hashtables.

Uploaded by

hahqg91
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

LAB 8 Data Structures

HASHTABLES Spring 2025

1
AGENDA

▪ Unordered Containers in STL.


▪ What is Unordered Map?
▪ Unordered Map member functions.
▪Task_1: Letter Counter
▪Task_2: Employees payroll system
STL: UNORDERED MAP
▪ Unordered map is implemented as a hash table.
▪ Associative: elements are referenced by their key and not
by their absolute position in the container.
▪ Unordered: elements are organized using hash tables
that allow for fast retrieval of elements by their key.
▪ Map: each element associates a key to a mapped value.
▪ Unique Keys: No two elements in the container can have
equivalent keys.
▪ Elements are not sorted in any particular order, but
organized into buckets. Which bucket an element is placed
into depends entirely on the hashing of its key.
UNORDERED MAP MEMBER
FUNCTIONS
▪ empty: Test whether container is empty
▪ size: Return container size
▪ begin: Return iterator to beginning
▪ end: Return iterator to end
▪ operator[]: Access element
▪ at: Access element
▪ find: Get iterator to element (end if not found)
▪ erase: erase elements
▪ clear: clear all elements
▪ swap: swap content with another unordered map.
HOW TO USE UNORDERED_MAP
#include
<unordered_map>
Creating Object of unordered_map:
unordered_map<keyDataType, valueDataType> myMap;

Inserting data to the map:


1. myMap[key]=value;
2. cin>>myMap[key];
3. myMap.insert(make_pair(key,value));

Access value using key:


cout<< myMap[key];
cout<< myMap.at(key);
HOW TO USE UNORDERED_MAP
To iterate over the map:
unordered_map<keyDataType, valueDataType>:: iterator it;
for (it = myMap.begin(); it != myMap.end(); it++)
Or
unordered_map<keyDataType, valueDataType>:: iterator it = myMap.begin();
While (it != myMap.end())
it++;

Accessing key and value from iterator:


it->first or (*it).first
it->second or (*it).second
Search for value using key:
unordered_map<keyDataType, valueDataType>:: iterator it = myMap.find(key);
If (it == end) : value is not found
Else : you can access the value by it->second or (*it).second
HOW TO USE UNORDERED_MAP
void main()
{
unordered_map<string, int> my_marks = {
{"Maths", 90},
{"Physics", 87},
{"Chemistry", 98} };

for (auto it = my_marks.begin(); it !=my_marks.end();


my_marks.at("Maths") += 10; it++)
my_marks.at("Physics") = 97;
my_marks["Chemistry"] += 20;
unordered_map<string, int>::iterator it;
for (it = my_marks.begin(); it != my_marks.end(); it++)
{
cout << (*it).first << ": " << (*it).second << endl; Output:
Maths: 100
}
Physics: 97
return 0; Chemistry: 118
TASK 1

Letter Counter
Write a program that:
▪ When given a string of letters, find frequencies of
individual chars.
▪Input : str = “helloworld";
▪Output : Frequencies of individual letters are (h, 1) (e, 1)
(l, 3) (o, 2) (w, 1) (r, 1) (d, 1)
void main()
{
unordered_map<char, int> wordCounter;
string sentence = "";
cout << "Enter a sentence: ";
cin >> sentence;

unordered_map<char, int>::iterator it;

for (int i = 0; i < sentence.size(); i++)


{
it = wordCounter.find(sentence[i]);
if (it != wordCounter.end())
wordCounter[sentence[i]]++;
else
wordCounter[sentence[i]] = 1;
}

for (it = wordCounter.begin(); it != wordCounter.end();


it++)
cout << it->first << " : " << it->second <<
endl;
TASK 2

Employees Payroll System


▪ A company has several employees , where each one has a unique
identity number, name, year of employment and salary.
▪The user can choose to update a certain employee’s salary or to upgrade
all employees salaries.
▪Press_1: To update a certain employee, the user should enter the ID and
the new salary of the employee. Print name and new salary.
void updateSalary (double newSalary)

▪ Press_2: To upgrade the salary of all the employees who had been
employed by the company for 10 years or more by a given increase rate.
The user should enter the increase rate. Print names and new salaries.
void Upgrade(float increase)

▪ Write a program for handling such system using STL hashtable.


class Employee
{ public:
int id;
string name;
double salary;
int employment_year;
Employee(int i, string n,double s, int y)
{
id = i;
name=n;
salary=s;
employment_year=y;
}
Employee()
{
id = 0; name="";
salary=0;
employment_year=0;
}
void updateSalary (double newSalary)
{
salary = newSalary;
}
void upgrade(double increase)
{
if((2024 - employment_year) >=10)
salary += (salary*increase/100);
}};
void main()
{
unordered_map<int,Employee> employees;

Employee e1(123,"Mohamed",3200,2005);
Employee e2(234,"Nour",5600,2010);
Employee e3(345,"Meena",8900,2008);

employees[e1.id]=e1;
employees[e2.id]=e2;
employees[e3.id]=e3;

int choice;
cout << "To Update An Employee: Press 1" << endl <<
"Upgrade All The Employees: Press 2" << endl;
cin >> choice;
switch (choice)
{
case 1:
{ int ID;
double Sal;
cout << "Enter ID and New Salary to Update
Employee Salary" << endl;
cin >> ID >> Sal;
employees[ID].updateSalary(Sal);
cout << employees[ID].name << " " <<
employees[ID].salary;
break;}
case 2:
{ double increaseRate;
cout << "Enter The Increase Rate" << endl;
cin >> increaseRate;
for (auto it = employees.begin();it !=
employees.end();it++)
{ (*it).second.upgrade(10);
cout << (*it).second.name << " " <<
(*it).second.salary << endl;
}}
15

You might also like