Demo - Hash table
// main.cpp
// HashMapDemo
// Created by Kevin Roark on 3/29/24.
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
// Initialize the unordered_map to store student names and their attendance count
unordered_map<string, int> attendance;
// Simulate attendance records
attendance["Kevin"] = 12; // Kevin has attended 12 classes
attendance["Sally"] = 15; // Sally has attended 15 classes
attendance["Sam"] = 9; // Sam has attended 9 classes
// Increment attendance for Alice
attendance["Kevin"]++;
// Check and print the attendance
cout << "Attendance records:" << endl;
for (const auto& pair : attendance) {
cout << pair.first << " has attended " << pair.second << " classes." << endl;
}
// Check if a student (David) is in the record
string studentName = "David";
if (attendance.find(studentName) != attendance.end()) {
cout << studentName << " has attended " << attendance[studentName] << " classes." << endl;
} else {
cout << studentName << " is not found in the attendance records." << endl;
}
// Check if a student (Kevin) is in the record
string studentNameTwo = "Kevin";
if (attendance.find(studentNameTwo) != attendance.end()) {
cout << studentNameTwo << " has attended " << attendance[studentNameTwo] << " classes." << endl;
} else {
cout << studentName << " is not found in the attendance records." << endl;
}
return 0;
}
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark