Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Entrepreneurship

Authors: Andrew Zacharakis, William D Bygrave

5th Edition

1119563097, 9781119563099

More Books

Students also viewed these Programming questions