Question: Write a c++ class `Map` that is templated on both the Key and the Value type, pretty much like a std::map. However, this class is

Write a c++ class `Map` that is templated on both the Key and the Value type, pretty much like a std::map. However, this class is different from a std::map, in that values must additionally be unique in the map as well, not just the keys. You have to write some basic functions that can add key-value pairs to an object of this class, as well as some functions that can query the keys and values in the objects.

The add method allows keys, and values to be added in any order. All test cases will use different types for the key and value. The querying functions simply return a default value of the key/value if the corresponding value/key is missing. Unlike a std::map there is no default insertion behavior when the key/value is missing.

No loops are allowed here, you need to write these functions with standard algorithms.

Please take into consideration the test cases shown in the above pictures, as the code evaluation will be on these test cases. No main is needed.

First test case:

Map m; ASSERT_EQ(m.size(), 0); m.add(2, \"hello\"); ASSERT_EQ(m.size(), 1); m.add(3, \"bye\"); ASSERT_EQ(m.size(), 2); ASSERT_EQ(m.value(3), \"bye\"); ASSERT_EQ(m.key(\"hello\"), 2); ASSERT_EQ(m.value(4), \"\"); ASSERT_EQ(m.key(\"ho\"), 0); m.add(\"aloha\", 5); ASSERT_EQ(m.value(5), \"aloha\"); ASSERT_EQ(m.key(\"aloha\"), 5); // ***IGNORE BELOW*** // no loops check #include #include #include std::ifstream code{\"code.cpp\"}; std::regex r{R\"~(\\bfor\\b|\\bwhile\\b|\\bgoto\\b)~\"}; std::string line; while (std::getline(code, line)) ASSERT_TRUE(!std::regex_search(line,r));,>

Second test case:

Map m; ASSERT_EQ(m.size(), 0); m.add(2, \"hello\"); ASSERT_EQ(m.size(), 1); m.add(3, \"bye\"); ASSERT_EQ(m.size(), 2); ASSERT_EQ(m.key(3), \"bye\"); ASSERT_EQ(m.value(\"hello\"), 2); ASSERT_EQ(m.key(4), \"\"); ASSERT_EQ(m.value(\"ho\"), 0); m.add(\"aloha\", 5); ASSERT_EQ(m.key(5), \"aloha\"); ASSERT_EQ(m.value(\"aloha\"), 5); m.add(\"aloha\", 6); ASSERT_EQ(m.key(5), \"aloha\"); ASSERT_EQ(m.value(\"aloha\"), 5); // ***IGNORE BELOW*** // no loops check #include #include #include std::ifstream code{\"code.cpp\"}; std::regex r{R\"~(\\bfor\\b|\\bwhile\\b|\\bgoto\\b)~\"}; std::string line; while (std::getline(code, line)) ASSERT_TRUE(!std::regex_search(line,r));,>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!