This project contains a template-based Map class in C++ which is implemented using two custom List objects to store keys and values.
- Insert: Add a new key-value pair to the map. If the key already exists, its value is updated.
- Remove: Remove a key-value pair from the map using the key.
- ValueOf: Retrieve the value associated with a given key.
- Print: Print all key-value pairs in the map.
- Size: Get the number of key-value pairs in the map.
- GetKeys: Retrieve a list of all keys in the map.
- GetValues: Retrieve a list of all values in the map.
- A working C++ compiler
list.hfile that contains the implementation of theListclass template used for storing keys and values.
Include the list.h file in your project and compile the program using a C++ compiler.
Here's a simple example of how to use the Map class:
#include "list.h"
#include "map.h"
#include <iostream>
using namespace std;
int main() {
Map<string, int> myMap;
// Inserting key-value pairs
myMap.insert("apple", 5);
myMap.insert("banana", 10);
myMap.insert("cherry", 15);
// Updating value for an existing key
myMap.insert("apple", 7);
// Printing the map
myMap.print(); // Output: {apple : 7, banana : 10, cherry : 15}
// Retrieving value by key
int value = myMap.valueOf("banana");
cout << "Value of banana: " << value << endl; // Output: Value of banana: 10
// Removing a key-value pair
myMap.remove("cherry");
// Printing the map after removal
myMap.print(); // Output: {apple : 7, banana : 10}
return 0;
}Inserts a key-value pair into the map. If the key already exists, updates its value.
Removes the key-value pair associated with the given key. Prints an error message if the key is not present.
Returns the value associated with the given key. Prints an error message and returns NULL if the key is not present.
Prints all the key-value pairs in the map.
Returns the number of key-value pairs in the map.
Returns a list of all keys in the map.
Returns a list of all values in the map.