Complete the code to declare the size of the hash table array.
static const int TABLE_SIZE = [1];The hash table size is set to 10 to create an array of 10 buckets.
Complete the code to compute the hash index for a given key.
int hashFunction(const std::string& key) {
int hash = 0;
for (char ch : key) {
hash += ch;
}
return hash [1] TABLE_SIZE;
}The modulo operator (%) ensures the hash value fits within the table size.
Fix the error in the insert function to add a new key-value pair correctly.
void insert(const std::string& key, int value) {
int index = hashFunction(key);
for (auto& pair : table[index]) {
if (pair.first == key) {
pair.second = value;
return;
}
}
table[index].[1](std::make_pair(key, value));
}push_back adds the new key-value pair to the end of the vector at the bucket.
Fill both blanks to complete the remove function that deletes a key from the hash map.
void remove(const std::string& key) {
int index = hashFunction(key);
auto& bucket = table[index];
for (auto it = bucket.[1](); it != bucket.[2](); ++it) {
if (it->first == key) {
bucket.erase(it);
return;
}
}
}begin() returns iterator to start; end() returns iterator past last element for loop traversal.
Fill all three blanks to complete the get function that returns the value for a key or -1 if not found.
int get(const std::string& key) {
int index = hashFunction(key);
for (const auto& pair : table[[1]]) {
if (pair.[2] == key) {
return pair.[3];
}
}
return -1;
}Use 'index' to access the bucket, 'first' for the key, and 'second' for the value in the pair.
