Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Here is a C + + class definition for an abstract data type Map from string to double, representing the concept of a function mapping

Here is a C++ class definition for an abstract data type Map from string to double, representing the concept of a function mapping strings to doubles. (For example, we could represent a collection of students and their GPAs: "Fred" maps to 2.956, "Ethel" maps to 3.538, "Lucy" maps to 2.956, etc.) We'll call the strings the keys and the doubles the values. No two keys in the map are allowed to be the same (e.g., so "Fred" appears no more than once as a key), although two keys might map to the same value (as in the example, where both of the keys "Fred" and "Lucy" map to the value 2.956). To make things simpler for you, the case of letters in a string matters, so that the strings Fred and fReD are not considered duplicates.
class Map
{
public:
Map(); // Create an empty map (i.e., one whose size() is 0).
bool empty(); // Return true if the map is empty, otherwise false.
int size(); // Return the number of key/value pairs in the map.
bool insert(const std::string& key, const double& value);
// If key is not equal to any key currently in the map and if the
// key/value pair can be added to the map, then do so and return true.
// Otherwise, make no change to the map and return false (indicating
// that either the key is already in the map, or the map has a fixed
// capacity and is full).
bool update(const std::string& key, const double& value);
// If key is equal to a key currently in the map, then make that key no
// longer map to the value that it currently maps to, but instead map to
// the value of the second parameter; in this case, return true.
// Otherwise, make no change to the map and return false.
bool insertOrUpdate(const std::string& key, const double& value);
// If key is equal to a key currently in the map, then make that key no
// longer map to the value that it currently maps to, but instead map to
// the value of the second parameter; in this case, return true.
// If key is not equal to any key currently in the map, and if the
// key/value pair can be added to the map, then do so and return true.
// Otherwise, make no change to the map and return false (indicating
// that the key is not already in the map and the map has a fixed
// capacity and is full).
bool erase(const std::string& key);
// If key is equal to a key currently in the map, remove the key/value
// pair with that key from the map and return true. Otherwise, make
// no change to the map and return false.
bool contains(const std::string& key);
// Return true if key is equal to a key currently in the map, otherwise
// false.
bool get(const std::string& key, double& value);
// If key is equal to a key currently in the map, set value to the
// value in the map which the key maps to, and return true. Otherwise,
// make no change to the value parameter of this function and return
// false.
bool get(int i, std::string& key, double& value);
// If 0<= i < size(), copy into the key and value parameters the
// key and value of the key/value pair in the map whose key is strictly
// greater than exactly i keys in the map and return true. Otherwise,
// leave the key and value parameters unchanged and return false.
void swap(Map& other);
// Exchange the contents of this map with the other one.
};
(When we don't want a function to change a key or value parameter, we pass that parameter by constant reference. Passing it by value would have been perfectly fine for this problem, but we're requiring you to use the const reference alternative because that will be more suitable after we make some generalizations in a later problem.)
Notice that the comment for the get function implies that for a 5-item map mm, mm.get(0, x, y) will copy the least key in the map into x (and its corresponding value into y), because the least key is greater than 0 keys in the map); mm.get(4, x, y) will copy the greatest key (and its corresponding value), because the greatest item is greater than 4 items in the map). The words greater than, least, etc., are all interpreted in the context of what the > operator for string indicates about the relative order of two strings:
Map mm;
mm.insert("Little Ricky", 3.206);
mm.insert("Ethel",3.538);
mm.insert("Ricky",3.350);
mm.insert("Lucy",2.956);
mm.insert("Ethel",3.538);
mm.insert("Fred",2.956);
mm.insert("Lucy",2.956);
assert(mm.size()==5); // duplicate "Ethel" and "Lucy" were not added
string x;
double y;
mm.get(0, x, y);
assert(x == "Ethel"); // "Ethel" is greater than exactly 0 items in mm
mm.get(4, x, y);
assert(x == "Ricky"); // "Ricky" is greater than exactly 4 items in mm
mm.get(2, x, y);
assert(x =="

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started